Abstract Class Concept Simplified in Java



The more we go towards class hierarchy,the more we create just the structure to create the sub classes. These are more rules than actual.so upwards the higher in hierarchy classes are more abstract classes.  When a class provides a common interface for all the classes derived from it,the class provides some dummy as well as original methods.Abstract classes are such classes in java which contains one or more abstract methods in it.

Here all threes classes inherits the shape class.The shape class is created to serve a common class. The shape class serves in our goal of achieving inheritance and polymorphism.But it was not built to be instantiated. Such class can be labelled as Abstract class.An abstract class exists only to express the common properties of all its sub classes so that they can be expressed differently for each different sub types.It establishes a basic form,so that we can say what is common with all the derived classes.
An abstract class is created when we want to manipulate a set of classes through this common interface.
Three classes Rectangle,Circle and Triangle have Display(),CalculateArea() these two common methods but they act differently as context changes. Here the abstract class Shape is the parent of these classes and has these methods. Each class by inheritance will get these methods and they need to implement as per their context.
An abstract class can be structured as

abstract class Shape{
...
...
}
So we can not perform the following operation for abstract class Shape.An abstract class is one that is not used to construct objects but only provides a base for making sub classes.If we do that compiler will throw an error message.

Shape obj=new Shape();
 
rather we have to do the following:
A class that is not abstract said to be concrete class .We can create objects belonging to a concrete class but not to an abstract class.

Rectangle objRect=new Rectangle();
Circle objCir=new Circle();
Triangle objTrian=new Triangle();
 
It is possible that we do not announce Shape class as abstract and we instantiate it. But in that case object will have no use in our code and may cause potential errors.In this example,Shape and Display() method are semantically abstract.we can redesign the class with an abstract modifier to tell java that Shape class is an abstract class.
abstract methods:
When final classes can not be sub classed and final methods can not be redefined,java allows opposite to that by using abstract keyword. This indicates that a method must always be redefined in a subclass thus making overriding compulsory. 
An abstract method is an incomplete method that has just the method definition but does not contain implementation. It is kind of dummy method.In this context,Display(),CalculateArea() methods are dummy methods.
signature will be:

abstract public void CalculateArea();//no definition only declaration
abstract public void Display();//no definition only declaration
 
Now these two methods for the above three classes()[Rectangle,Circle,Triangle] are different and have to be overridden in the respective classes. These methods does not have meaning in the Shape class.But we need to make sure that all the inheriting classes do have the method.Such methods can be labeled as abstract methods.So for a abstract class there is no need of implementation of the abstract methods.It is only the signature.If we have an abstract class,objects of this class almost always have no meaning,hence we need to prevent the users from doing so.Either can print an error message,but that delays the information till runtime and not reliable. The best solution of this problem is to make them abstract method.

Things to remember for abstract class

  1. An abstract class may also have a complete method.
  2. A class can be declared as abstract even if it does not contain any abstract method.(during design)
  3. Reference of an abstract class can point to objects of it's sub classes there by achieving the run time polymorphism.Exaple- Shape obj=new Rectangle();
  4. A class must be declared abstract if it has one or more abstract methods.
  5. A variable whose type is given by an abstract class can only refer to objects that belong to concrete sub class of the abstract class.
  6. An abstract class is not a concrete class ,we can not use extends keyword while deriving new classes from it. 
  7. If we inherit a class from an abstract class and we want to make objects of the new type,we must provide method definitions of all the abstract methods,in the base class. If we don't(we have an option here),then the derived class is also become an abstract class.
  8. All derived class methods that match the signature of the base class declaration will be called using the dynamic binding mechanism.If the method's name is the same as the base class but the argument are different then we will get overloading.
  9. We can not declare abstract constructor or abstract static methods.
Structure of the abstract Shape class

abstract class Shape{
abstract public void CalculateArea();
abstract public void Display();
}
 
Any subclasses of this needs to implement the abstract class

class Rectangle implements Shape{
//provide implementation of the methods of shape
Display(){}
CalculateArea(){}
}
 
Abstract Class Concept Simplified in Java Abstract Class Concept Simplified in Java Reviewed by Animesh Chatterjee on October 15, 2018 Rating: 5

No comments:

Powered by Blogger.