Abstract class in Java

Abstract class in Java
Spread the love

Abstract class in Java is the same as interface except that it can contain default method implementation. An abstract class can have both abstract methods ( without a body ) and normal methods ( with implementation body ) also.

Java has a special keyword called abstract. which is used to create an abstract class and method. Abstract class in java can’t be instantiated(means you can’t create object directly).

An abstract class is mostly used to provide a base for subclasses to extend and implement the abstract methods and override or use the implemented methods in abstract class

Abstract Class in Java

Here I am writing a simple example of an Abstract Class in Java.

package com.itechf.demo; //abstract class public abstract class Student { private String name; private String gender; public Student(String name, String gender){ this.name=name; this.gender=gender; } //abstract method example public abstract void todo(); @Override public String toString(){ return "Name="+this.name+" | Gender="+this.gender; } public void changeName(String name) { this.name = name; } }
Code language: JavaScript (javascript)

above example todo() is an abstract method and it has no-body.

Here is a concrete class example extending an abstract class in java.

package com.itechf.demo; public class MyStudent extends Student { private int id; public MyStudent(String name, String gender, int id) { super(name, gender); this.id=id; } @Override public void todo() { if(id == 0){ System.out.println("Not a Student"); }else{ System.out.println("He is a Student"); } } public static void main(String args[]) { //example abstract classes Student st = new Student("Pooja","Female"); Student st2 = new MyStudent("Amit","Male",32); st.todo(); st2.todo(); //using method implemented in abstract class - inheritance st2.changeName("Deepak Kumar"); System.out.println(st2.toString()); } }
Code language: JavaScript (javascript)

Note that subclass MyStudent inherits the properties and methods of superclass Student using inheritance in java.

Also notice the use of Override annotation in MyStudent class.

Abstract class in Java Important Points

  1. abstract the keyword is used to create an abstract class in java.
  2. Abstract class in java can’t be instantiated.
  3. We can use abstract keyword to create an abstract method, an abstract method doesn’t have a body.
  4. If a class have abstract methods, then the class should also be abstract using abstract keyword, else it will not compile.
  5. All the methods in an interface are implicitly abstract unless the interface methods are static or default. Static methods and default methods in interfaces are added in Java 8, for more details read Java 8 interface changes.
  6. Java Abstract class can implement interfaces without even providing the implementation of interface methods.
  7. Java Abstract class is used to provide common method implementation to all the subclasses or to provide a default implementation.
  8. We can run an abstract class in java like any other class if it has main() method.
  9. It’s not necessary for an abstract class to have an abstract method. We can mark a class as abstract even if it doesn’t declare any abstract methods.
  10. . If an abstract class doesn’t have any method implementation, it’s better to use an interface because java doesn’t support multiple class inheritance.
  11. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.

These are some basic ponts for an abstract class in Java.

Spread the love

One thought on “Abstract class in Java

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top