Teja
Teja

Reputation: 13534

Abstract keyword before Super Class Method in Method Over-riding

As Super Class objects cannot be instantiated in the main function abstract keyword is specified before the class name.But what difference does it make if an abstract keyword is used before the SuperClass over-riding method or not used. Can someone explain it please?

Here is the below example.Please check the commented part.

abstract class Figure
{
    int dim1;
    int dim2;

    Figure()
    {
        dim1=-1;
        dim2=-1;
    }

    Figure(int p,int q)
    {
        dim1=p;
        dim2=q;
    }

    abstract void Area() //This line is working without abstract for me.
    {
        System.out.println("The area is undefined.");
    }
}

class Rectangle extends Figure
{
    int vol;
    Rectangle()
    {
        super();
    }

    Rectangle(int p,int q)
    {
        super(p,q);
    }

    void Area()
    {
        vol=dim1*dim2;
        System.out.println("The area of the rectangle is: "+vol);
    }
}

class Triangle extends Figure
{
    int vol;
    Triangle()
    {
        super();
    }

    Triangle(int p,int q)
    {
        super(p,q);
    }

    void Area()
    {
        vol=dim1*dim2/2;
        System.out.println("The area of the rectangle is: "+vol);
    }
}


public class Area 
{
    public static void main(String[] args) 
    {

        Rectangle r=new Rectangle(10,20);
        Triangle t=new Triangle(6,10);
        Figure fref;

        fref=r;
        r.Area();
        fref=t;
        t.Area();

    }

}

Upvotes: 0

Views: 1215

Answers (5)

vinsinraw
vinsinraw

Reputation: 2125

  1. A class can have abstract as well as non abstract methods

  2. If you declare even a single abstract method in your class, you have to make your class abstract. (Note: abstract method means method declaration only without any body. In your case you have to select either of two options: a) Declare your method as: abstract void area(); b) Remove abstract keyword from both your method and class)

  3. If a class is abstract, it must be extended by another abstract class or concrete class. In your case you are doing in the form of Triangle and Rectangle which are extending Figure.

  4. All abstract method must be implemented by first concrete class in your inheritance tree. By concrete class means the class which you can instantiate. So if you have abstract method in your abstract class(say abstract void area();), then you have to implement it (provide body) in your concrete class(here Rectangle and Triangle in your case).

As a rule of thumb: If your super class is not abstract then you have the option either to override its method in subclass or not. But in case of abstract class you must override it in your first concrete class else compiler will throw you an error.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500903

With the abstract modifier, that's invalid code - you can't specify the body of an abstract method at the point you declare it. It should just be:

abstract void Area();

(That's leaving aside the violation of naming conventions etc.)

The whole point of an abstract method is to force subclasses to provide an actual implementation, usually because the abstract class itself doesn't know how to. If you can provide an implementation within the abstract class, consider just making it a normal method instead, which can be overridden by subclasses if they wish to.

Note that it's possible to have an abstract class without an abstract method, but it's relatively unusual.

See section 8.4.3.1 of the Java Language Specification and the "abstract" part of the Java tutorial for more information.

Upvotes: 2

Shivan Dragon
Shivan Dragon

Reputation: 15219

It has to do with type contracts.

First, you can make an abstract class with no abstract methods. This sais "this clas cannot be instantiated as it is, you have to extend it and then instantiate THAT class (as long as THAT class is not abstract as well).

Then, you can make a method abstract. This sais "i'm not gonna give any implementation for this method, I want to force anyone who extends this class to supply an implementation for that method".

Now, since abstract methods have no implementation in the class that declares them, when you make a method abstract the compiler compels you to make its class abstract aswell, because you it doesn't make sense to instantiate that class directly when it has at least one methods with no implementation (the abstract method).

Upvotes: 0

Azodious
Azodious

Reputation: 13872

abstract void Area();

abstract means that it's body will be defined in it's derived class.

if you try to define a body for it, it'll be compiler time error.

So, as a rule of thumb:

An abstract class can have two kinds of methods: with body and without body. The methods with body cannot be prefixed with abstract keyword. However methods without body must be prefixed with abstract keyword.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691785

Marking a method abstract means that the class defines the method, but does not implement it. It forces the concrete subclasses to provide an implementation of the method.

Either you provide an implementation, and the method may not be abstract, or you don't provide one, and the method must be abstract (and thus the class must also be).

Read the Java tutorial.

Upvotes: 0

Related Questions