Hope anyone can help, i am learning Java and as comparable to anyone else in this forum i guess i am a newbie to programming as well.
I have come across a chapter on abstract class and methods but don't really fully understand what they are used for and why, and thought i would get an explanation from someone who is an experienced programmer.
Below i have example code i have been working on, with the help from this book, what i am not sure about is why in the class Dims do i have to have abstract double area() when each sub class uses an area method anyway, or is it calling the area method from the super class, why do you have to have methods that override?
// Using abstract methods and classes
package Training2;
abstract class Dims {
double dim1;
double dim2;
Dims(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}
class Rectangles extends Dims {
Rectangles(double a, double b) {
super(a, b);
}
// Override area for Rectangles
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangles extends Dims {
Triangles(double a, double b) {
super(a, b);
}
// Override area for right Triangles
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 /2;
}
}
public class AbstractAreas {
public static void main(String args[]) {
// Dims d = new Dims(10, 10); // Illegal now
Rectangles r = new Rectangles(9, 5);
Triangles t = new Triangles(10, 8);
Dims dimref; // This is OK, no object is created
dimref = r;
System.out.println("Area is " + dimref.area());
dimref = t;
System.out.println("Area is " + dimref.area());}
}
Apologies for the waffling on but i really need some guidance.
Thanks in advance.
We do this to say "This class has a complete (ish) API, but it does not have a complete implementation." Among other things, it means that you can do this:
public void doSomething(Dims d) {
System.out.println("The area is " + d.area());
}
// ...using it somewhere:
doSomething(new Triangle(4.0, 6.0));
In doSomething
, even though the reference we have to the object is a Dims
, not a Triangle
, we can call area
(and we end up calling Triangle#area
) because it's defined in the Dims
API (sometimes called a "contract"). It's just that Dims
defers the implementation to subclasses. Which is why you can't create instances of abstract classes. The doSomething
method doesn't have any idea whether what it was given is a Triangle
or Rectangle
, just that it's some kind of Dims
.
Whereas if Dims
didn't define area
, doSomething
wouldn't compile. We'd have to declare a doSomething
accepting a Triangle
and another one accepting a Rectangle
and so on. We couldn't get the benefit of being able to operate on all Dims
things in common code.
There's a lot of crossover between interfaces and abstract classes in Java. Fundamentally, you can think of an abstract class as an interface with a partial implementation (with the caveat that a class can implement more than one interface, but can only derive from a single abstract class). The line's gotten even blurrier now that interfaces can define "default" implementations of methods. (Some argue that now that interfaces can have default methods, they're the "new" abstract classes and make true abstract classes obsolete as a language feature, but there are still arguments, mostly syntactic, around using abstract classes sometimes.)
Each shape that expends Dims
need a different algorithm to calculate the area, the area of a rectangle is not the same as the area of a triangle. Thus we declare the method abstract in Dims
to force each shape to implement it own algorithm to properly calculate area.
Declaring the method abstract in Dims
force the shape to override it, else they will be abstract object themself. That way we are sure that calling .area
on any Dims
will result in the correct calculation.
Abstract class force you to show behavior that it have as abstract method. So you have to override abstract methods in your subclasses.
It works as an interface. And you have to do implementation.
Here you can't remove areas method from you Rectangles class
Refer Java™ Tutorials from Oracle; Good example for in depth understanding.
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
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, then the subclass must also be declared abstract.
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html