Question

I am new to Java (reading books for 4 months now). So probably my question can appear too simple. My understanding is that abstract methods don't have a body and can't provide implementation

So how does this works?

public abstract void fillRect (int x, int y, with, height);

I did't point the question clearly. We've got abstract method. Why does it draw a rectangle if I doesn't provide a body, just parameters.

For example

public void paint (Graphics g) {

g.fillRect (5, 5, 30, 30);

}
Was it helpful?

Solution 2

There are two things you need to know

-declaration : The prototype or structure of the method. e.g:

    public int add(int a, int b);

-definition : The implementation of the method

    public int add(int a, int b) {
        this.a = a;
        this.b = b;
        return a + b;
    }

Now an abstract method can have a declaration i.e a structure or prototype. But it cannot have a definition. The definition should be done in the class which extends the class containing the abstract method:

    class A {
        public abstract int add(int a,int b); //just declaration- no body
    }
    class B extends A {
        /*must override add() method because it is abstract in class A i.e class B must have a body or definition of add()*/ 
        int a, b;
        public int add(int a,int b) {
            this.a = a;
            this.b = b;
            return a + b;
        }
    }

OTHER TIPS

Also read docs

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

You can see abstract classes and abstract methods as a sort of contract. If you have an abstract class (any class with an abstract method becomes an abstract class), you can not create an object of that class.

You might wonder what the use is of abstract classes in such a case. That's where the contract comes in to play. Let's say you have a class Vehicle, with a method drive. You want all vehicles to be able to drive, but you don't know how yet (a car drives in a different way than a bycicle).

That's why you create the method

public abstract void drive();

and implement it in your subclasses Car and Bycicle.

Granted that all animals have from 0 to n legs:

abstract Class Animal{

    public abstract int legCount();

}

Class Dolphin extends Animal{

    public int legCount(){ return 0; } 

}

Class Cat extends Animal{

    public int legCount(){ return 4; } 

}

Class Man extends Animal{

    public int legCount(){ return 2; } 

}

Every specie will have its different way to count legs/paws - you just can't do for an abstract animal, because there is no abstract animal at all, just concrete specie individuals.

Abstract methods used when one method can implement in more than one sub class with different implementation.

sub-class is responsible to provide different implementation of the abstract methods.

Note:= One abstract class extends another abstract class.

note:= We cannot create a abstract class objcet.

If a class has an abstract method, you cannot instanciate an object from it. You can instanciate an object from a derived class implementing the abstact methods.

However, you can specify that an argument of some other method/function is of an "abstract class", e.g.,

doSomethingWithIt(MyAbstractClass argument);

and the method doSomethingWithIt only relies on the fact that the object passed to it provides an implementation of the abstract methods in MyAbstractClass, but it does not rely on which derived class is providing that implementation.

Hence, abstract methods are useful, e.g., if you like to use different implementations for the same method.

This is a core technique of OO and I assume all books are covering it.

following

public abstract void fillRect (int x, int y, with, height);

doesn't work with abstract class alone only. It will work with subclass.

Make a subclass of abstract class and then override it with implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top