Understanding Abstraction in Java

Share your love

Index

What is Abstraction in Java?

Real World Example of Abstraction in Java

Ways of Achieving Abstraction in Java.

Abstract Class in Java

abstract void moveTo(double deltaX, double deltaY);
public abstract class GraphicObject {

   // declare fields

   // declare nonabstract methods

   abstract void draw();

}

Key points to Remember

<em>class MyClass extends AnotherClassName implements InterfaceName {&nbsp; }</em>
Abstract Class

Understanding Abstract Class with the help of a code

abstract class Animal {

    // Abstract method (no body)

    abstract void makeSound();

    // Concrete method

    void sleep() {

        System.out.println("Zzz");

    }

}

class Dog extends Animal {

    // Implementation of abstract method

    void makeSound() {

        System.out.println("Woof");

    }

}

class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.makeSound(); // Outputs "Woof"

        dog.sleep();     // Outputs "Zzz"

    }

}

Abstract Class

abstract class Animal {

    // Abstract method (no body)

    abstract void makeSound();

    // Concrete method

    void sleep() {

        System.out.println("Zzz");

    }

}

Concrete Subclass

class Dog extends Animal {

    // Implementation of abstract method

    void makeSound() {

        System.out.println("Woof");

    }

}

Main Method

class Main {

    public static void main(String[] args) {

        Dog dog = new Dog();

        dog.makeSound(); // Outputs "Woof"

        dog.sleep();     // Outputs "Zzz"

    }

}

Abstract Method

Difference between Abstract and Concrete Methods
abstract class ClassName {

    abstract returnType methodName(parameters);

}
abstract void makeSound();

Key points to remember

Abstract Method

Interface

public interface Animal {

    void makeSound(); // abstract method

    void sleep(); // abstract method

}
The diamond Problem

Key Points

interface A {   }  ===  abstract interface A {   }
interface A { 

      // int id;  // Compilation error

      int id = 20;

   }
interface A { 

     int calulateArea();

   }

 interface B { 

     int calulateArea();

   }     

 

 class MyClass implements A, B {

   public int calulateArea() {

      // Defining once is enough.

    } 

 }
interface A { 

     int calulateArea();

   }

 interface B { 

     void calulateArea();

   }     

 class MyClass implements A, B {

    // Not possible to define calculateArea() for both interfaces   

 }
interface MyInterface { 

     void print();

   }    

 

 class MyClass implements MyInterface {

    public void print() {

        System.out.println("Inside print method");

     }

    public void message() {

        System.out.println("Inside message method");

     }

    public static void main(String args []) {

      MyInterface obj = new MyClass(); // Assigning MyClass object into MyInterface type

      // obj.message(); // Compilation error

      obj.print(); // prints "Inside print method"

    }

 }

This behavior is intentional and is a fundamental concept of polymorphism and abstraction in object-oriented programming. By assigning an object to an interface type, we can effectively treat the object as an instance of the interface, which can only access the members (methods and properties) defined in the interface contract.

interface X { 

     int id = 10;

   }

 interface Y { 

     int id = 20;

   }     

 

 class MyClass implements A, B {

   public static void main(String args []) {

      // System.out.println(id); //  compilation error

      System.out.println(X.id); // Access interface A variable, prints 10

      System.out.println(Y.id); // Access interface B variable, prints 20

    }

 }
interface A { 

      int id = 35;

      void test()

  } 

 class Myclass implements A {

    public void test() {

       id = 30; // Compilation error

     }

  }
Interface

Understanding Interface with the help of code

public interface Vehicle {

    // Constants

    double MAX_SPEED = 200.0; // public, static, and final by default

    // Abstract methods

    void start();

    void accelerate(double speed);

    void brake();

    double getCurrentSpeed();

}

The Implementation Part 

public class Car implements Vehicle {

    private double currentSpeed;

    @Override

    public void start() {

        System.out.println("Car started.");

    }

    @Override

    public void accelerate(double speed) {

        currentSpeed += speed;

        System.out.println("Car accelerated to " + currentSpeed + " km/h.");

    }

    @Override

    public void brake() {

        currentSpeed = 0;

        System.out.println("Car stopped.");

    }

    @Override

    public double getCurrentSpeed() {

        return currentSpeed;

    }

}
currentSpeed += speed

The Main Method

public class Main {

    public static void main(String[] args) {

        Vehicle myCar = new Car();

        myCar.start();

        myCar.accelerate(50.0);

        System.out.println("Current speed: " + myCar.getCurrentSpeed() + " km/h");

        myCar.brake();

    }

}

Nested Interface 

Syntax

interface first{  

     interface second{  

         // body

     }  

}
class abc{

    interface _interface_name{

       // body

    }

}

Difference between Abstract class and interface

Difference between Abstract class and interface

Similarity between Abstract Class and Interface

When to use Abstract Class VS When to use interface

Advantages of Abstraction in Java

Disadvantages of Abstraction in Java

Conclusion

Q2. What is the difference between Encapsulation and Abstraction

Ans. Encapsulation is the practice of bundling data and methods within a single unit, like a class, and controlling their access, whereas abstraction is about hiding complex implementation details and exposing only the essential functionalities.

Q3. What is the benefit of using abstract Abstract classes?
Q4. Can we create an object of the Abstract class?

Ans. No, we cannot create an object of an abstract class directly. We can only create objects of subclasses that extend the abstract class and provide implementations for its abstract methods.

Q5. Can an abstract class have a constructor?

Ans. Yes, an abstract class can have a constructor. The constructor is called when an instance of a subclass is created. This is useful for initializing common properties that are shared among all subclasses.

Q6. Can an abstract class be final in Java?

Ans. No, an abstract class cannot be declared as final in Java because a final class cannot be subclassed, and an abstract class needs to be subclassed to provide implementations for its abstract methods.

Q7. Why use interfaces when we have abstract classes?

Ans. The reason is that abstract classes may contain non-final variables, whereas variables in the interface are final, public, and static.

Share your love

One comment

  1. The level of my appreciation for your work mirrors your own enthusiasm. Your sketch is visually appealing, and your authored material is impressive. Yet, you appear to be anxious about the possibility of moving in a direction that may cause unease. I agree that you’ll be able to address this matter efficiently.

Leave a Reply

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