Abstraction explained with example

Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details.The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.

Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is.

In java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces.

  • An abstract class is a class that is declared with abstract keyword.
  • An abstract method is a method that is declared without an implementation.
  • An abstract class may or may not have all abstract methods. Some of them can be concrete methods
  • A method defined abstract must always be redefined in the subclass,thus making overriding compulsory OR either make subclass itself abstract.
  • Any class that contains one or more abstract methods must also be declared with abstract keyword.
  • There can be no object of an abstract class.That is, an abstract class can not be directly instantiated with the new operator.
  • An abstract class can have parametrized constructors and default constructor is always present in an abstract class.

When to use?
There are situations in which we will want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes we will want to create a superclass that only defines a generalization form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.

Encapsulation vs Data Abstraction

  • Encapsulation is data hiding(information hiding) while Abstraction is detail hiding(implementation hiding).
  • While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing the interface to the user and hiding the details of implementation.
package com.basic;

public class AbstractionDemo {

	public static void main(String[] args) {
		//Creating object of Men and accessing Men class methods along with Human class methods
		Men m=new Men(); 
		m.beard();   //Men class method
		m.superman(); //Men class method
		m.walk(); //Human class method but defined in Men class which is abstract
		m.eat(); // Human class method which is concrete method
		
		// Similarlywith Women class object accesing women class methods and Human class methods
		Women w=new Women(); 		
		w.longHair();
		w.reproduction();
		w.walk();
		w.eat();
		
		//Human class reference but Men class object
		Human h=new Men(); 
		h.eat();
		h.walk();
		
		//Human class reference but Women class object
		Human h2=new Women();
		h2.eat();
		h2.walk();
	}
}

abstract class Human{ //Abstract class with one abstract method
	
	void eat() {
		System.out.println("Human Eats food");
	}
	//One abstract method with no body, has to be defined in class which extends
	abstract void walk() ; 
}

//Men class extends Human so only abstract method must be defined
class Men extends Human{
	void beard() {
		System.out.println("Men has beard");
	}
	void superman() {
		System.out.println("Sumerman is a MAN");
	}
	void walk() {   //This method is abstract in Human so here it must be defined
		System.out.println("WALK: MEN walks fast");
	}
}

//Women class extends Human so only abstract method must be defined	
class Women extends Human{ 
	void longHair() {
		System.out.println("Women have long hair");
	}
	void reproduction() {
		System.out.println("Women can reproduce");
	}
	void walk() {   //This method is abstract in Human so here it must be defined
		System.out.println("WALK: WOMEN walks slow");
	}
}

Credits:
Naveen Reddy (YouTube Channel)
geeksforgeeks.org

Leave a Reply