abstract keyword in java -


abstract Keyword -

Always remembers the following points about abstract -
  • Abstract can be classes and methods. We cannot declare variables as abstract.
  • We cannot create objects of abstract classes using new keyword.
  • An abstract class may or may not contain abstract methods.
  • If a class contains any abstract method then it is compulsion to declare the class as abstract.
  • An abstract method does not have any body. We have to override them if we want to work with them.

Example 1 -

class First
{
}

abstract class Second
{
}

public class Call {
    public static void main(String[] args) {
        
        First f1 = new First();             // Works fine
        Second s1 = new Second(); // Error - Second is abstract; cannot be instantiated
    }
} 

   
In the above example we cannot create object of Second class because it is abstract.

Example 2 -

abstract class Marks
{
    private int phy, chem;

    public void input()
    {
        phy = Integer.parseInt(System.console().readLine("Enter physics marks"));
        chem = Integer.parseInt(System.console().readLine("Enter chemistry marks"));
    }
    
    public void disp()
    {
        System.out.println(phy+"    "+chem);
    }
}

class Temp extends Marks
{
}        
        
public class Call {

    public static void main(String[] args) {
      
        Temp t1 = new Temp();
        t1.input();
        t1.disp();
    }
    
}

Output -

    Enter physics marks     80
    Enter chemistry marks  90
    80     90
In the above example Marks class is abstract, so we cannot create objects of this class. In such cases if we want to access members of the class, then we can inherit the class. So we create a second class named Temp and inherit Marks class. Now all public members of Marks class are also available in Temp class, so we can access them by creating objects of Temp class.

Example 3 -

abstract class Marks
{
    private int phy,chem;
   
    public void input()
    {
        phy = Integer.parseInt(System.console().readLine("Enter physics marks"));
        chem = Integer.parseInt(System.console().readLine("Enter chemistry marks"));
    }
    
    public void disp()
    {
        System.out.println(phy+"    "+chem);
    }
    
    abstract public void show();   //abstract method
}

class Temp extends Marks
{
    public void show()
    {
        System.out.println("Just a Demo");
    }
}        
        
public class Call {

    public static void main(String[] args) {
      
        Temp t1 = new Temp();
        t1.input();
        t1.disp();
        t1.show();
    } 
}

Output -

    Enter physics marks     80
    Enter chemistry marks  90
    80     90
    Just a Demo
In the above example Marks class contain an abstract method named show(), And always remember that if a class contain any abstract method then that class should also be abstract. In this case if we do not override show() method in Temp class, then we also have to declare Temp class abstract.
Because Temp is inheriting Marks class, so show() method is also available in Temp class and as rule says we have to declare class abstract if it contain any abstract method.
So we override show() method and now no need to declare Temp class as abstract.