Abstraction in Java | OOP Fundamental Concept

Topic – 1 : Abstraction

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

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message

An Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data Abstraction is defined as the process of reducing the object to its essence so that only the necessary characteristics are exposed to the users.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

  1. Abstract class (0 to 100%)
  2. Interface (100%)

In Java, we can achieve Data Abstraction using Abstract classes and interfaces.

Abstract Class

Points to Remember

Syntax of declaring an abstract class:

access-specifier abstract class ClassName

{

//class body

}

Abstract Methods in Java

•   A method which is declared as abstract and does not have implementation is known as an abstract method.

•   The child classes which inherit the abstract class must provide the implementation of these inherited abstract methods.

access-specifier abstract return-type method_name();

public abstract void printStatus();

Example – 1

public void move(){System.out.println(“Car moves faster.”);}
public class Car extends Vehicle {
public abstract class Vehicle { public abstract void move();}

An abstract class that has abstract method

Example – 2
An abstract class that has abstract , non-abstract methods and Constructor
 public abstract class Vehicle { public class Car extends Vehicle {
public Vehicle(){System.out.println(“Vehicle is Created.”);}public void move(){System.out.println(“Car moves faster.”);}
public abstract void move(); public void carry(){System.out.println(“All Vehicle carry loads”);}public static void main(String[] args) { Car c1 = new Car();c1.move();c1.carry();}}
}// Outputs:Vehicle is Created. Car moves faster.All Vehicle carry loads
 

Interface in Java

static constants.

Internal addition by the compiler

Syntax of declaring an Interface

interface <interface_name>{

// declare constant fields

// declare methods that abstract

//by default.

}

The relationship between classes and interfaces

As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface

Example – 1

public class SIBL implements Bank{ public double account (){System.out.println(“Account System in SIBL”);}}
class TestInterface{public static void main(String[] args){SIBL b=new SIBL();b. account ();DBBL d=new DBBL();d. account ();}}
interface Bank{ double account();}
public class DBBL implements Bank{ public double account (){System.out.println(“Account System in DBBL”);}}

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as

multiple inheritance

Example – 2

interface FirstInterface { public void myMethod();}

class DemoClass implements FirstInterface, SecondInterface {

public void myMethod() {

System.out.println(“Some text..”);

}

interface SecondInterface {public void myOtherMethod();}

Thank you!

Leave a Reply

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