[!INDEX]

  1. Definition + Hiding the complex implementation details..
  2. Abstract Classes + abs mt + concrete mt ( animal , dog)
  3. Benefits of Abstraction
  4. method without body : must declare abstract
  5. inherit abs. cls. without implement its mt(). body : + Vehicle+ start + car
  6. code : above correct code + vehicle + car
  7. code : make abs. cls. obj. in main m/t. + vehicle + car + scooter
  8. code : above correct code abs. class variable instantiate subclass

1. Def. Hiding the complex implementation details..

[!NOTE]

  1. Hiding the complex implementation details and showing only the necessary features of an object. This is achieved through abstract classes and interfaces.
  2. An abstract method in Java is a method that does not have a body (i.e., it has no implementation) and is meant to be overridden by subclasses. It is declared with the abstract keyword and ends with a semicolon instead of braces {}.
  3. Abstract methods are defined in abstract classes or interfaces. They act as a placeholder, ensuring that any concrete subclass provides a specific implementation of the method.
    • An abstract class in Java is a class that cannot be instantiated on its own and is meant to be subclassed. It can have both abstract methods (without implementation) and concrete methods (with implementation).
  1. No body/implementation:
  2. Declared in an abstract class or interface:
  3. Must be overridden:
  4. Access modifiers:

2. Abstract Classes + abs mt + concrete mt ( animal , dog)

package package2;
abstract class Animal {
    abstract void makeSound(); // Abstract method (does not have a body)
    void sleep() {
        System.out.println("Sleeping...");     // Regular method
    }
}
class Dog extends Animal {
    @Override
    void makeSound() { // body defined
        System.out.println("Bark");
    }
}
public class Main1 {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound();
        myDog.sleep();
    }
}
Bark
Sleeping...

3. Benefits of Abstraction

[!NOTE]

  1. Reduces Complexity: By hiding the unnecessary details, abstraction simplifies the use of complex systems.
  2. Improves Maintainability: Changes in the abstracted part of the code do not affect the higher-level modules, making the code easier to maintain.
  3. Enhances Code Reusability: Abstracted code can be reused across different parts of the application or even in different projects.
  4. Promotes Loose Coupling: By relying on abstract interfaces rather than concrete implementations, the system becomes more modular and easier to extend or modify.

4. method without body : must declare abstract

public class Vehicle {
    void start();  
}

[!important]

  1. Methods without a body must be declared as abstract.
  2. If a class contains an abstract method, the class itself must also be declared as abstract.
  3. This indicates that the class cannot be instantiated on its own and is intended to be subclassed.

5. inherit abs. cls. without implement its mt(). body : + Vehicle+ start + car

package package2;
abstract class Vehicle {
    abstract void start();  
}
public class Car extends Vehicle {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.start();
    }
}
The type Car must implement the inherited abstract method H02.start()Java(67109264)

6. code : above correct code + vehicle + car

package package2;
abstract class Vehicle {
    abstract void start();  
}
public class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car is starting...");
    }
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.start();
    }
}
Car is starting...

7. code : make abs. cls. obj. in main m/t. + vehicle + car + scooter

abstract public class Vehicle { 
    abstract void start(); 
} 
class car extends Vehicle { 
    void start(){ 
        System.out.println("starts with key"); 
    } 
} 
class scoters extends Vehicle { 
    void start(){ 
        System.out.println("starts with kick"); 
    } 
    public static void main(String[] args) { 
        Vehicle t = new Vehicle(); 
    } 
} // H02 is abstract; cannot be instantiated

[!important]

  1. you cannot create an instance of an abstract class directly.
  2. However, you can create instances of its subclasses, which implement the abstract methods.

8. code : above correct code abs. class variable instantiate subclass

abstract public class Vehicle {
    abstract void start();  
}
class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("starts with key");
    }
}
class Scooters extends Vehicle {
    @Override
    void start() {
        System.out.println("starts with kick");
    }
    public static void main(String[] args) {       
        Vehicle myCar = new Car(); // Correct way: instantiate subclasses
        myCar.start(); // Outputs: starts with key

        Vehicle myScooter = new Scooters();
        myScooter.start(); // Outputs: starts with kick
    }
}