Learn Abstract Class in Java in a Simple and Fast Manner

3
177
digitization, transformation, binary
Spread the love

Abstract Class in Java

A class is having a method definition but not implementation is called Abstract class. So if we derive one class from an abstract class we must provide the implementation for every method defined in the abstract class. If we do not do it our new class is to be treated as an abstract class and we won’t be able to create an instance of that class.

The more we go towards class hierarchy, the more we create just the structure to create the subclasses. 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 contain one or more abstract methods in it.

If we make a method final, Java ensures that that method cannot be redefined in any of its subclasses. The abstract keyword does opposite to this. Abstract keyword enforces us to redefine the method in any of its subclasses.

This scenario happens when the parent class provides a general definition of a method and asks the subclasses to create their specific codes.

Syntax of an abstract class-

abstract return–type methodname (parameterlist);

If a class has one or more abstract method(s), the class itself becomes abstract, we can put the abstract keyword in front of the class definition. We cannot create any object of the abstract class. (no new operator can be used)

Note- Constructors cannot be declared as abstract. There is no provision for abstract static methods.

READ  Heap Concept Simplified In Java

Hence, any subclass (child class ) needs to implement all the methods declared as abstract in the parent class. If it fails to do so, the child class itself becomes another abstract class.

abstract class Test {
abstract void launchBrowser( );
abstract void test( );
abstract void closeBrowser ( );
}
Class MyTest extends Test{
void launch Browser( ){
 //…  
}
 void test ( ){            
//''''
}
void closebrowser(){
//''''
}
}

Here all threes classes inherit 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 a class can be labelled as an Abstract class. An abstract class exists only to express the common properties of all its subclasses so that they can be expressed differently for each different subtypes. 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.

Abstract Class
Abstract Class

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 subclasses. 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 a 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 subclassed 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 a kind of dummy method.In this context,Display(),CalculateArea() methods are dummy methods.
a 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 do 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 labelled as abstract methods. So for an abstract class, there is no need for 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 to this problem is to make them abstract method.

READ  BigDecimal Class in Java

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 its subclasses thereby achieving the run time polymorphism.Example- 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 the concrete subclass 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 also becomes 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 is 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 need to implement the abstract class


class Rectangle implements Shape{
//provide implementation of the methods of shape
Display(){}
CalculateArea(){}
}
 


Spread the love
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

3 COMMENTS

  1. […] Learn Abstract Class in Java in a Simple and Fast Manner Read More » […]

  2. […] Learn Abstract Class in Java in a Simple and Fast Manner Read More » […]

  3. […] Learn Abstract Class in Java in a Simple and Fast Manner Read More » […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here