What is an abstract class?

An abstract class is a class that is partially implemented. It provides design convenience. An abstract class consists of one or more abstract methods that are declared but left unimplemented. It is the responsibility of subclasses that extend an abstract class to implement the unimplemented part of the abstract class. As the implementation of an abstract class is not complete, it is not possible to directly create objects of an abstract class.

A class containing one or more abstract methods must be declared as abstract. However, a class can also be declared as abstract even if it has no abstract methods. An abstract class may also have non-abstract methods.

  • Share/Bookmark

abstract class Ques0347A{
int x;
int init(){
x = 15;
return x;
}
abstract void disp();
}

public class Ques0347 extends Ques0347A{
void disp(){
int y = init();
System.out.println(y);
}
public static void main(String[] argv) {
Ques0347 ob = new Ques0347();
ob.disp();
}
}

An abstract class can be inherited as same as a simple class by using the extends clause. In the given code, there are two classes named Ques0347A and Ques0347. The Ques0347A class is declared as abstract because it contains a declaration of an abstract method named disp. It also contains an instance variable x and an instance method init().

  • Share/Bookmark
  1. If a class does not implement any or all of an interface method, then it should be declared as abstract.
  2. An abstract class is made up of one or more abstract methods that are declared but left unimplemented.
  3. Abstract classes cannot be instantiated, but they can be subclassed.
  4. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
  • Share/Bookmark