Abstract class in Java

In this tutorial, we will learn about the Abstract class in Java. The main motive of creating an abstract class is to restrict the user to create an object of the class. Abstract classes can only be used with the help of inheritance means we can inherit the abstract class in class. Java provides us “abstract” which we can use to make a class abstract. When we define an abstract method in abstract class then we don’t provide the body of the method. we define body in child class while inheriting abstract class.

The main reason why we make some classed abstract because at the time of making some classes we don’t know exact working of some methods so we create some abstract methods in the class. And we don’t want to allow any user to create an object of this incomplete class

Important things to know about Abstract class:

  • The abstract keyword is used to create an object of the class.
  • An abstract class may have or may not have abstract methods
  • We can not create an object of abstract class we can only inherit it.
  • If there is an abstract method in abstract class then after inheritance we have to override abstract(to provide its working) method. Otherwise, the compiler will show an error.

Here we will see an example in which we will create an abstract class called Person. In Person class we will create a abstract method called work and then we will create two other classes called Student and Teacher.

Example Program:

abstract class Person{
     String name;
     int age;
     public void add( String name,int age){
         this.name=name;
         this.age=age;
     }
     //abstract methoth work 
     abstract public void work();
  
     public void show(){
        System.out.println("Hello "+name+" your age is "+age);
        work();
     }
}

public class Student extends Person{
  //overriding abstract method work
  public void work(){
    System.out.println("Your work is to study");
  }
   public static void main(String args[]){
     Student obj=new Student();
     obj.add("Abc",20);
     obj.show();
   }
}

Output:

Hello Abc your age is 20
Your work is to study

If we will not override abstract work method in Student class. Then compiler will show us an error:

Student is not abstract and does not override abstract method work() in Person
public class Student extends Person{
             ^ 
1 error

Now we will implement the same Person class in Teacher class and here we will again override the work method but with the different code.

Example Program:

abstract class Person{
     String name;
     int age;
     public void add( String name,int age){
         this.name=name;
         this.age=age;
     }
     //abstract method work
     abstract public void work();
  
     public void show(){
        System.out.println("Hello "+name+" your age is "+age);
        work();
     }
}

public class Teacher extends Person{
  //overriding abstract method work
  public void work(){
    System.out.println("Your work is to teach students");
  }
   public static void main(String args[]){
     Teacher obj=new Teacher();
     obj.add("XYZ",30);
     obj.show();
   }
}

Output:

Hello XYZ your age is 30
Your work is to teach students

 

Spread the love
Scroll to Top
×