Let’s see what is an abstract class in Java. Consider below hierarchy of object creation for mobile manufacturing company.

If company wants to create new model of any device (any of sub class type), then it can be created using new object instantiation. It makes sense to create new iPhone, new android, new iPad and new tablet but what exactly is the Phone object? What type it is? What configuration it is? What processor does it have….

Trying to create an object of type Phone is something bad idea. But how do we deal with this? We need a Phone class for inheritance and polymorphism. But we want programmers to instantiate only the less abstract subclasses of class Phone, not Phone itself. We want iPhone object, andriod object but not Phone objects.

There’s a simple way to prevent a class from ever being instantiated. In other, to stop anyone from saying “new” on that type. By marking the class as abstract, the compiler will stop any code, anywhere, from ever creating an instance of that type.

Some classes just should not be instantiated, are called as abstract class.

When you’re designing your class inheritance structure, you have to decide which classes are abstract and which are concrete. Concrete classes are those that are specific enough to be instantiated. Concrete class just means that it’s OK to make objects of that type.

Making a class abstract is easy — put the keyword abstract before the class declaration.

public abstract class Iphone extends Phone{

  public void setModel() {
    
  }
}

An abstract class means that nobody can ever make a new instance of that class. You can still use that abstract class as a declared reference type, for the purpose of polymorphism, but you don’t have to worry about somebody making objects of that type. The compiler guarantees it.

A subclass object to a super-class reference even if the super-class is abstract is acceptable but the abstract class instantiation cannot be done as in above example.

An abstract class has virtually no use, no value, no purpose in life, unless it is extended. There is an exception to this, an abstract class can have static members.

Abstract Methods

Besides classes, you can mark methods abstract, too.

An abstract class means the class must be extended;

An abstract method means  the method must be overridden.

An abstract method has no body! So no curly braces — Just end the declaration with a semicolon.

public abstract class IphoneX extends Iphone{

  public abstract void setVersion();
}

If you declare an abstract method, you MUST mark the class abstract as well. You can’t have an abstract method in a non-abstract class.

But you can mix both abstract and non-abstract methods in abstract class.

Abstract methods don’t have a body; they exist solely for polymorphism. That means the first concrete class in the inheritance tree must implement all abstract methods. What you put in the implementing the method is up to you. All Java cares about that is the method is there, in your concrete subclass.

Conclusion 

We saw what is abstract class in Java. How to use it and abstract method usage in class and its limitation in Java.

Please follow and like us: