What is Java abstract method abstract class with Example?

What is Java abstract method abstract class with Example?

I explain the Java abstract method and abstract class in a different style where it is easier to understand the concept.

One day, the Chief Design Engineer of Maruthi Suzuki thought every vehicle that rolls out of factory should have some common features. He designs an abstract class as follows and demands all the Design Engineers of all models to inherit the class.

Declaring Java abstract class

Observe the above class. Two methods exist without body – automaticDoorlock() and stereo(). The Java rule says, if a method does not have body, it must be informed to the compiler; else compiler thinks the body is missing by mistake by the Programmer and thereby does not compile the program (by rule, definition of method declaration says, the method should have a body which is missing here). This is done with abstract keyword. That is to say, if a method does not have body, the method should be declared as abstract, else it is a compilation error.

Body misses means no output or no functionality. Then who will give body to the abstract method? The subclasses who extends Vehicle should give the body, else again compilation error.

Maruthi Swift Design engineer overrides the Java abstract methods in the following manner.

Let us see how Maruthi Alto Design engineer overrides the Java abstract methods.

The same Java abstract methods of Vehicle are implemented (given body) by the respective Design Engineers of Swift and Alto differently as per their design and need. If it is the case, what abstract methods serve the purpose in Java coding? They say what to do but not how to do. How to do is the job left to the subclass as per its requirement and need of the hour. Observe, the same methods of Vehicle are overridden differently by two subclasses.

The abstract methods give a template of methods from which new methods can be developed easily (without thinking what methods should exist and what meaningful names should be given). The method is abstract means, body or implementation is abstracted (not given).

Then where to place the Java abstract methods? Any special class exists? No much special class. Just declare the class abstract where abstract methods exist. That is, to say in a rule format, the class with abstract methods should be declared abstract.

Let us format a few rules for abstract methods and abstract classes.

  1. Abstract method should not have a body.
  2. If one of the methods of a class is abstract, the class should be declared abstract.
  3. All the abstract methods of the abstract class should be overridden (should be given body) by subclass.
  4. With abstract class, we cannot create objects. That is, abstract classes cannot be instantiated.
  5. An abstract class can be subclassed. Abstract class means for the usage of other classes.
  6. By chance, if the subclass does not provide body for the super class abstract methods, then the subclass should be declared abstract.

An abstract class have three variations.

1. An abstract class can contain all abstract methods.
2. An abstract class can contain all concrete (non-abstract) methods.
3. An abstract class can contain a mixture of abstract methods and concrete methods.

1. Why the Java abstract class cannot be instantiated?

Naturally, it is of common sense. If an object of Vehicle is created and called automaticDoorlock() method, what output to be given? No output. For this reason, Designers preferred not allow to create an instance of an abstrct class.

2. Can an abstract method declared as private?

No, impossible. It is compilation error. A private access specifier says the method cannot be inherited by inheritance by a subclass. But there is no meaning if the abstract method cannot be overridden. For this, reason an abstract method cannot be private.

3. Can an abstract class have reference variables?

Yes, no problem. You cannot create objects of abstract class but you can create reference variables.

4. Can an abstract class have a constructor and main() method?
5. Can an abstract method declared static?

Examples and explanation for these questions are given in the following links.

Following programs are available, in way2java.com, with explanation and screenshots on abstract classes for further elaborate reading and understanding in depth including the 3 variations of abstract classes.

1. Java Abstract Classes
2. Abstract class constructor & Interface abstract
3. Abstract Static Method
4. Difference between Abstract class and Interface
5. Can we instantiate an abstract class?
6. Difference between abstract class and final class
7. Abstract class with main()

One thought on “What is Java abstract method abstract class with Example?

Leave a Reply

Your email address will not be published. Required fields are marked *