简体   繁体   中英

Area of an object- Abstract Class - Java

I am learning Java using the book Java: The Complete Reference . Currently I am working on the topic of abstract classes.

Please Note: There are similar questions on stackoverflow. I searched them but I couldn't understand the concept.

If I run the below program, it produces the correct output, but I didn't understand the concept.

What is the need of reference variable of an Abstract class here. I can get the output without the reference variable of an abstract class.

First I ran the below program and got the desired output.

abstract class Figure {
  double dim1;
  double dim2; 

  Figure(double a, double b) {
    dim1 = a;
    dim2 = b;
  }

  // area is now an an abstract method 
  abstract double area();
}

class Rectangle extends Figure {
  Rectangle(double a, double b) {
    super(a, b);
  }

  // override area for rectangle
  double area() {
    System.out.println("Inside Area for Rectangle.");
    return dim1 * dim2;
  }
}

class Triangle extends Figure {
  Triangle(double a, double b) {
    super(a, b);
  }

  // override area for right triangle
  double area() {
    System.out.println("Inside Area for Triangle.");
    return dim1 * dim2 / 2;
  }
}

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

    Rectangle r = new Rectangle(9, 5);
    Triangle t = new Triangle(10, 8);

    Figure figref; 

    figref = r;
    System.out.println("Area is " + figref.area());

    figref = t;
    System.out.println("Area is " + figref.area());
  }
}

And I tried the below code without creating/using abstract class reference.

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

    Rectangle r = new Rectangle(9, 5);
    Triangle t = new Triangle(10, 8);

    // Figure figref; 

    // figref = r;
    System.out.println("Area is " + r.area());

    // figref = t;
    System.out.println("Area is " + t.area());
  }
}

It also gave the same output as the first program.

Can anyone please explain what is the need of calling "area method" using abstract class reference.

It's meant simply as a demonstration that even though you declared the variable as the abstract type, you can assign an instance of a concrete subclass to it and get the overriden behavior from the subclass.

Practical use example would be if you needed a collection of them:

List<Figure> figureList = new ArrayList<Figure>();
figureList.add(new Rectangle(9, 5));
figureList.add(new Triangle(10, 8));

for (Figure f : figureList) {
    System.out.println(f.area());
}

Or if you want to pass any subclass of Figure to a method that used the area() :

public void printArea(Figure f) {
    System.out.println("Area is: " + f.area());
}
...
myObject.printArea(new Rectangle(9, 5));
myObject.printArea(new Triangle(10, 8));

In Abstract classes you can define abstract as well as non abstract methods. However the 1st concrete subclass of the Abstract class must implement those abstract methods. You cannot create instance of Abstract classes and they must be subclassed to some concrete class.

Also note JLS states if abstract classes have all abstract method it is better to use interface.

Can anyone please explain what is the need of calling "area method" using
abstract class reference.

Concept is same as inheritance. We use abstract classes to avoid duplicate.

What is the need of reference variable of an Abstract class here. I can get the
output without the reference variable of an abstract class.

Abstract class is used as a reference because you can take advantage of polymorphism here. If you call area() on the reference variable at runtime it will call the corresponding implementation of Traingle or Rectangle based on the actual instance type.

Hey here you're using a concept of UPCASTING which is also known as Parent reference to the child object . And the above code program which u have written is performing UPCASTING . Let us look what is UPCASTING .

Upcasting is a mechanism of using parent class reference to refer the child class objects .

Whenever you use Upcasting you can access only the parents class members ( both variables and methods) and the overridden methods of parent class .

In your example the method area() has been overidden in the child classes Rectangle and Triangle so they can be accessed using parent reference figref .

One of the advantage of UPCASTING is we can achieve Dynamic Method Dispatch or Dynamic Polymorphism which is very much necessary in writing complex applications having complex class hierarchies.

Since u mentioned you're using Complete reference Check out the section Dynamic Method Dispatch which comes after method overriding .

Hope this answer Helps :)

Yes you can get the same answer But it is always preferred to use abstract classes or intefaces to call any api. area() is an api which is overridden in Rectangle or Triangle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM