☰ Topics

Abstract Class

Abstraction refers to showing only the necessary details to the intended user. It focuses on the outside view of an object. It is the concept of hiding irrelevant details. In other words, make complex system simple by hiding the unnecessary detail from the user. In java, We achieve abstraction by using Interface and Abstract class.
Abstract class: This class is compulsorily inherited further. Here, methods are also abstract. There are situation in which we want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. Any class that contains one or more abstract methods must also be declared abstract. We cannot declare abstract constructors (no objects of abstract class is made, so constructor won't be called) or abstract static methods (static methods can't be inherited, it is general for all class). Any subclass of an abstract class must either implement all of the abstract methods of the super class or be itself declared abstract. Some points are:-

  1. To make abstract class, we have to add abstract key with that class.
  2. We cannot create the object of abstract classes.
  3. Any class that contains one or more abstract method must also be declared abstract.
  4. We cannot declare the abstract constructor and abstract static methods.
  5. Subclass of an abstract class have to override all the abstract method of an abstract class otherwise it have to make itself abstract.
  6. Abstract classes can have constructors, member variables and concrete or normal methods.
  7. Method that are declared without body within an abstract class is known as abstract method. It can never be final and static.
Example:-
abstract class A{
    abstract void check();
    void show(){
	    System.out.println("this is a concrete method");
	}	
}
class Sum extends A{
	void check(){
	    System.out.println("Implementation of abstract method");
	} 
}
class D1{
	public static void main(String d[]){
	    Sum c=new Sum();
		c.check();
	    c.show();
	}
}
OUTPUT:

Implementation of abstract method
this is a concrete method

Note:- Partial abstraction is achieved by Abstract class because it can have concrete methods. If Final keyword is used with the methods then methods won't be overridden further, if used with variables then it will become constant & should be initialized there only.