Day-27:Abstraction and interface

**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 we type the text and send the message. We do not know the internal processing about the message delivery. Abstraction lets you focus on what the object does instead of how it does it. Points to Remember An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructors and static methods also. It can have final methods which will force the subclass not to change the body of the method.[TBD] Abstract Method in Java A method which is declared as abstract and does not have implementation is known as an abstract method. Example of abstract method abstract void printStatus();//no method body and abstract Example of Abstract Class that has an Abstract Method In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class. //Creating an abstract class having abstract method abstract class Bike{ abstract void run(); } //Creating a child class and override abstract method class Honda extends Bike{ void run(){System.out.println("running safely");} } //Creating a Main class to create object and call methods public class Main{ public static void main(String args[]){ Bike obj = new Honda(); obj.run(); } } Output: running safely Interface An interface in Java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not a method body. It is used to achieve abstraction and multiple inheritance in Java. In other words, we can say that interfaces can have abstract methods and variables. It cannot have a method body. Java Interface also represents the IS-A relationship. It cannot be instantiated just like the abstract class. Since Java 8, we can have default and static methods in an interface. Since Java 9, we can have private methods in an interface.[TBD] Why use a Java interface? There are mainly three reasons to use an interface. They are given below. It is used to achieve abstraction. By interface, we can support the functionality of multiple inheritance. It is used to achieve loose coupling. How to declare an interface? An interface is declared by using the interface keyword. It provides total abstraction; it means all the methods in an interface are declared with an empty body, and all the fields are public, static and final by default[TBD]. A class that implements an interface must implement all the methods declared in the interface.[TBD] Reference link: https://www.tpointtech.com/abstract-class-in-java https://www.tpointtech.com/interface-in-java

Apr 17, 2025 - 14:27
 0
Day-27:Abstraction and interface

**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 we type the text and send the message. We do not know the internal processing about the message delivery.

Abstraction lets you focus on what the object does instead of how it does it.

Points to Remember

An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the body of the method.[TBD]

Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an abstract method.

Example of abstract method

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

Example of Abstract Class that has an Abstract Method

In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.

    //Creating an abstract class having abstract method  
    abstract class Bike{    
      abstract void run();    
    }    
    //Creating a child class and override abstract method  
    class Honda extends Bike{    
    void run(){System.out.println("running safely");}    
    }  
    //Creating a Main class to create object and call methods  
    public class Main{  
    public static void main(String args[]){    
     Bike obj = new Honda();    
     obj.run();    
    }    
    }   

Output:

running safely

Interface
An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not a method body. It is used to achieve abstraction and multiple inheritance in Java.

In other words, we can say that interfaces can have abstract methods and variables. It cannot have a method body.

  • Java Interface also represents the IS-A relationship.
  • It cannot be instantiated just like the abstract class.
  • Since Java 8, we can have default and static methods in an interface.
  • Since Java 9, we can have private methods in an interface.[TBD]

Why use a Java interface?

There are mainly three reasons to use an interface. They are given below.

  • It is used to achieve abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It is used to achieve loose coupling.

How to declare an interface?

An interface is declared by using the interface keyword. It provides total abstraction; it means all the methods in an interface are declared with an empty body, and all the fields are public, static and final by default[TBD]. A class that implements an interface must implement all the methods declared in the interface.[TBD]

Reference link:
https://www.tpointtech.com/abstract-class-in-java
https://www.tpointtech.com/interface-in-java