Abstract Class and Abstraction in Java.

We are going to discuss the abstract class. Before learning the Java abstract class, we are going to discuss the Abstraction and Abstract method.

 

What is Abstraction in Java? 

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

The main purpose of abstraction is to hide the unnecessary details from the users.

I another way, we can say abstraction is shown only essential things to the user and hides the internal details from the user.

Let’s try to understand with an example, we are withdrawing money from ATM, so we are entering the ATM card and pin number and amount and you get the money, but we don’t know what is internally going on in this process.

so in this process, we are only able to see essential things like entering a password, enter amount, but we are not able to see the internal process of that transaction.

 

How to achieve Abstraction in java.

There are two ways to achieve abstraction in java.

  • Abstract class (0 to 100%)
  • Interface (100%)

 

When we are using Abstract class then we are not able to achieve 100% abstraction. Because in Abstract class we are using the abstract method and non-abstract method also.

When we are using Interface than we are able to achieve 100% abstraction. Because in Interface we are using the only abstract method. But after Java 8 we are able to non-abstract method also.

 

What is Abstract method?

If you want to write our method but you don’t know the definition of the method you just know the name of the method and the number of parameters it will take, so what we can do is we can declare the methods just write the method name, parameters, and a semicolon.

If you declare a method in Java-like (methods without body) that method directly becomes abstract so those methods are called abstract methods.

An abstract method is a method that has just the method definition but does not contain implementation.

abstract void goingTo();//no method body and abstract 

 

What is Abstract class?

A class that is declared using the “abstract” keyword is known as an abstract class.

An abstract class can have abstract methods (“methods without a body”), and also concrete methods (“methods with the body”).

In abstract class, you can not get the object, so the first point to remember for abstract classes you cannot create object of an abstract class.

An abstract class can have abstract methods it’s doesn’t mean you cannot have an abstract class without abstract methods.

abstract class Example{} 

 

Java program of Abstract class that has an abstract method

//abstract Class 
abstract class Car {
	abstract void run(); // no method body and abstract
}

class Maruti extends Car {
	void run() { //implementing abstract method
		System.out.println("running safely");
	}

	public static void main(String args[]) {
		Car obj = new Maruti();
		obj.run();
	}
}

Output: 

running safely


abstract class Game { abstract void play(); } //In real scenario, implementation is provided by others i.e. unknown by end user class Cricket extends Game { void play() { System.out.println("playing Cricket"); } } class FootBall extends Game { void play() { System.out.println("playing FootBall"); } } //In real scenario, method is called by programmer or user class ExampleAbstraction { public static void main(String args[]) { Game objCricket = new Cricket(); // creating Cricket class object objCricket.play(); Game objFootBall = new FootBall(); // creating Cricket class object objFootBall.play(); } }

Output:

playing Cricket
playing Foot Ball


abstract class Mobile { abstract int getPrice(); } class Redmi extends Mobile { int getPrice() { return 5000; } } class Samsung extends Mobile { int getPrice() { return 6000; } } class ExampleMobile { public static void main(String args[]) { Mobile m; m = new Redmi(); System.out.println("Price of Redmi mobile is: " + m.getPrice()); m = new Samsung(); System.out.println("Price of Samsung mobile is: " + m.getPrice()); } }

Output:

5000
6000

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

Leave a Reply

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