Loading

Abstraction means hiding the background detail and showing the essential element only. Abstraction is not possible without inheritance as method overriding is not possible without inheritance.

Example:

Let's take a daily life example of a switchboard, we know that by tapping the switches we can turn them on and off the fan without knowing the functionality performed inside of the switch.

Need of Abstraction

  • Enhances security by hiding sensitive implementation details.
  • Reduces complexity and makes code easier to manage.
  • Prevents direct access to implementation, reducing the risk of accidental modifications.

Abstract Class in Java

An abstract class is a class that cannot be instantiated and may contain abstract methods (methods without a body).


Example:

abstract class AbstractClass {             // abstract class
	abstract void getAbstractClass();      // abstract method
}

Why Can't We Instantiate an Abstract Class ?

Since an abstract class may have abstract methods, it does not provide a full implementation. This means we cannot create objects of an abstract class directly.

Example:

package quipohouse;
public abstract class AbstractClass {
	public static void main(String[] args) {
		AbstractClass a = new AbstractClass();
		}


Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Cannot instantiate the type AbstractClass

How to Use an Abstract Class ?

If we want to access the non-static member of the abstract class then we have to create the object of a class that extends that abstract class.


Example:

package quipohouse;
abstract class AbstractClass {                                 // Abstract Class
	int a = 10;

	abstract void getAbstractMethod();                         // Abstract Method

	void getAbstractClass() {                                  // Concrete Method
		System.out.println("Inside Abstract Class");
	}
}

class Child extends AbstractClass {                            // Inheriting abstract class

// two public class cannot reside in one .java file
	@Override                                                  // Overriding abstract method 
	void getAbstractMethod() {
	}

	public static void main(String[] args) {                    
		Child c = new Child();                                 // Creating object
		System.out.println(c.a);                               // Calling abtract method through child
		c.getAbstractClass();
	}
}


Output:

10
Inside Abstract Class

Why Do We Need to Override Abstract Methods ?

If we don't want the child class to be abstract because of inheriting the abstract class containing abstract methods, then we must have to override all the abstract methods of parent classes.


Example:

package quipohouse;
abstract class AbstractClass {
	int a = 10;

	abstract void getAbstractClass();
}

class Child extends AbstractClass {
	@Override
	void getAbstractClass() {
		System.out.println("Inside Overriden Abstract Method");

	}

	public static void main(String[] args) {
		Child c = new Child();
		c.getAbstractClass();
		System.out.println();
	}
}


Output:

Inside Overriden Abstract Method

Abstract Class Constructor

For calling the constructor of an abstract class we have to instantiate the child class.


Example:

package quipohouse;
abstract class AbstractClass {
	public AbstractClass() {                        // Constructor
		System.out.println("Inside AbstractClass Default Constructor");
	}

	abstract void getAbstractMethod();
}

class Child extends AbstractClass {
	@Override
	void getAbstractMethod() {
	}

	public static void main(String[] args) {
		Child c = new Child();
		// as default constructor automatically called when we instantiate the class
	}

}


Output:

Inside AbstractClass Default Constructor

Abstraction can be done at two levels:



  • Class-Level               
  • Method-Level 


Abstract class or Class Level Abstraction:

  • At the class level, we achieve abstraction by declaring class abstract.
  • An abstract class have one or more abstract methods.
  • It may have non-abstract methods or concrete methods.
  • If a class has an abstract method then it must be declared as abstract.

Syntax:

public abstract Class_Name{
  abstract return_type method_Name();
  return_type method_Name(){
  }
}

Abstract Method or Method level Abstraction:

  • We do abstraction by creating abstract methods too.
  • An abstract method does not have a body or block and is declared abstract.
  • If an abstract class is extended by a child class then all abstract methods should be defined ( or have the body )which is also known as method overriding.

Syntax:

abstract return_type method_Name();


Examples of both:

package quipohouse;
abstract class AbstractClass {           //abstract class
	abstract void getAbstractClass();    //abstract method
	void getAbstractMethod() {
	
	}
}

 

Two Minute Drill

  • Abstraction hides unnecessary details and shows only what’s important.
  • You can’t create objects of an abstract class directly.
  • Abstract methods must be implemented in the child class.
  • If a child class doesn’t implement all abstract methods, it also becomes abstract.
  • Abstract classes can have both normal (with body) and abstract (without body) methods.
  • If an abstract class has a constructor, it runs when the child class object is created.