-2

Is abstraction possible without inheritance? This is my code

     abstract class whatever
{
    abstract void disp1();
    abstract void disp2();
    abstract void disp3();
}

 class what {

    void disp1()
    {
        System.out.println("This is disp1");
    }
}



public class threeClasses {

    public static void main (String args[])
    {
        what obj =new what();
        obj.disp1();

    }

}

Please note above, how i:

  1. did not extend the class "what" from abstract class "whatever" and yet the code runs perfectly with no errors
    1. Did not declare class "what" as abstract (since it's not declaring the other two methods disp2() and disp3())

I am very confused. Please help.

Asma Rahim Ali Jafri
  • 1,173
  • 2
  • 15
  • 22

4 Answers4

3

You aren't using whatever (and Java naming conventions should be respected). The idea behind an abstract class (and inheritance) is that there is an interface contract. Let's examine it with a more practical example,

abstract class Whatever {
    abstract void disp1();

    void disp2() {
        System.out.println("disp2");
    }

    void disp3() {
        System.out.println("disp3");
    }
}

Then make What extend it. Override two methods for demonstration (the annotation is a useful compile time safety check)

class What extends Whatever {
    @Override
    void disp1() {
        System.out.println("This is disp1");
    }

    @Override
    void disp2() {
        System.out.println("This is disp2");
    }
}

Finally, invoke methods on a What instance through the Whatever contract

public static void main(String args[]) {
    Whatever obj = new What();
    obj.disp1();
    obj.disp2();
    obj.disp3();
}

Which outputs

This is disp1
This is disp2
disp3

Note that What is providing the implementation for disp1 and disp2 while Whatever provides disp3.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

There is no relationship between your abstract class and your concrete class. Whatever your definition of "abstraction", it actually represents a relationship between types. The abstract keyword does not establish that relationship between classes, it represents that relationship, and not by itself. The relationship needs to be extended from both sides.

abstract is a declaration from one side about a promise that must be kept, for an inheriting type either to implement abstract methods or to ask for that promise from its inheriting types.

The other side makes the promise by being a class that inherits from the abstract type. Without inheritance, the concrete type loses the is-a connection.

You will get the compiler error you're complaining about missing if you correct one major mistake you made. You failed to use the @Override annotation. Always use the @Override annotation when you intend to override a method, or you will forever enjoy just the sort of bug you show here.

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10
0

I think what he meant was if we can implement abstract class's method without inheriting abstract class.

You might be thinking if we can do it with composition/association/aggregation relation.

To that, I will answer: NO because you can't create an object of abstract class as in these relations you have to make object or reference of the object.

So, the only way to implement abstract methods is through inheritance.

Shaheer
  • 50
  • 6
0

i think you are looking for some thing like my answer.

abstract class whatever {
    abstract void disp1();

    abstract void disp2();

    abstract void disp3();
}

public class threeClasses {

    public static void main(String args[]) {
        whatever obj = new whatever() {
            @Override
            void disp1() {
                System.out.println("This is disp1");
            }

            @Override
            void disp2() {
                System.out.println("This is disp2");
            }

            @Override
            void disp3() {
                System.out.println("This is disp3");
            }
        };
        obj.disp1();
    }
}

but also in this example you are creating an object from anonymous class that is inherited from whatever.

so i think your answer is abstraction can not be done without inheritance.

Amir
  • 1,638
  • 19
  • 26