[!INDEX]
- Definition + Hiding the complex implementation details..
- Abstract Classes + abs mt + concrete mt ( animal , dog)
- Benefits of Abstraction
- method without body : must declare abstract
- inherit abs. cls. without implement its mt(). body : + Vehicle+ start + car
- code : above correct code + vehicle + car
- code : make abs. cls. obj. in main m/t. + vehicle + car + scooter
- code : above correct code abs. class variable instantiate subclass
1. Def. Hiding the complex implementation details..
[!NOTE]
- Hiding the complex implementation details and showing only the necessary features of an object. This is achieved through abstract classes and interfaces.
- 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
abstractkeyword and ends with a semicolon instead of braces{}.- 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).
- Key Characteristics of an Abstract Method:
- No body/implementation:
- Declared in an abstract class or interface:
- Must be overridden:
- 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]
- Reduces Complexity: By hiding the unnecessary details, abstraction simplifies the use of complex systems.
- Improves Maintainability: Changes in the abstracted part of the code do not affect the higher-level modules, making the code easier to maintain.
- Enhances Code Reusability: Abstracted code can be reused across different parts of the application or even in different projects.
- 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]
- Methods without a body must be declared as abstract.
- If a class contains an abstract method, the class itself must also be declared as
abstract.- 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]
- you cannot create an instance of an abstract class directly.
- 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
}
}