Java Abstract Class - The Coding Shala

Home >> Learn Java >> Java Abstract Class

Abstraction in Java

Abstraction is an important pillar of object-oriented programming language. Abstraction is a process of hiding the implementation details and only showing the functionality to the user. Let's take an example Car. A user only knows how to drive, how to use breaks, how to change gears not all the details of how breaks work and how gear works. In Java when we know what object does not how the object does it, is called Abstraction.
Java Abstraction - The Coding Shala
There are two ways to achieve abstraction in Java:
  • Abstract class
  • Interface
In this chapter, we will see the Abstract class.

Java Abstract class

A class that is declared as abstract is known as an abstract class. We use the abstract keyword to declare an abstract class. An abstract class can have an abstract and non-abstract method. Abstract classes have partial implementation. We have to extend abstract classes to subclass and implement all the abstract methods in the subclass. 

Java Abstract Method

A method which is declared as abstract and does not the implementation is known as an abstract method. A subclass must have to override an abstract method to provide the implementation. 

The following syntax is used to declare the abstract class and abstract method in Java:

abstract class class_name{
    //body of class 
    
    abstract type method_name();  //this is abstract method
}

The following are important points to remember for Java Abstract classes:
  • Abstract classes must be declared with an abstract keyword.
  • Abstract classes can have abstract and non-abstract methods.
  • Abstract classes can have constructors and static methods also.
  • Abstract classes can have final methods also.
  • If a class contains an abstract method then that class must be abstract.
  • A class can not be declared with both final and abstract keywords. The final keyword is used to prevent overriding whereas abstract methods need to be overridden.
  • Abstract classes can not be instantiated. We can not create an object of abstract classes. It can only be used as a reference.

Example of Java Abstract class

The following example explains the Java Abstract class: 

//abstract class example
//thecodingshala.com 

abstract class Parent{
    abstract void Display();
}

class Child extends Parent{
    void Display(){
        System.out.println("Display method in Child class");
    }
}

class Main{
    public static void main(String[] args ){
        //Parent p1 = new Parent(); will give compile error 
        
        Parent p2 = new Child();
        p2.Display();
        
        Child c1 = new Child();
        c1.Display();
    }
}
Output: 

Display method in Child class
Display method in Child class

An abstract class can have a constructor also. A constructor of an abstract class called when an instance of child class is created. An abstract class can have no-abstract method also. The following example explains it: 

//abstract class example
//thecodingshala.com 

abstract class Parent{
 Parent(){
  System.out.println("Constructor of Parent class");
 }
    abstract void Display();
    
    void ClassName() {
     System.out.println("This is parent class");
    }
}

class Child extends Parent{
 Child(){
  System.out.println("Constructor of child class");
 }
    void Display(){
        System.out.println("Display method in Child class");
    }
   /* we can override non-abstract method in child class
    * @Override 
    void ClassName(){
     System.out.println("This is child class");
    }*/
}

class Main{
    public static void main(String[] args ){
        //Parent p1 = new Parent(); will give compile error 
        
        Parent p2 = new Child();
        p2.Display();
        p2.ClassName();
        
    }
}
Output: 

Constructor of Parent class
Constructor of child class
Display method in Child class
This is parent class

NOTE: if abstract class has final methods then final methods can not be overridden in child class.

An abstract class can exist without any abstract method but when an abstract method is there is a class then the class must be abstract. The following example explains it: 

//abstract class example
//thecodingshala.com 

abstract class Parent{
    void ClassName() {
     System.out.println("This is parent class");
    }
}

class Child extends Parent{
}

class Main{
    public static void main(String[] args ){
       
        Parent p = new Child();
        p.ClassName();
        
    }
}
Output: 

This is parent class

Question: Is it possible to create an abstract and final class in Java?
Answer: No, the final class can not have an abstract class because final classes can not override.

Question: Is it possible to have an abstract method in a final class?
Answer: No, when we declare an abstract method in a class the class automatically became abstract.


Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

Maximum Population Year LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala