Abstract-class-codes
Abstract-class-codes
// Regular method
void stop() {
System.out.println("The vehicle is stopping.");
}
}
OUTPUT:
The car is starting with a key.
The vehicle is stopping.
The motorcycle is starting with a button.
The vehicle is stopping.
EXPLANATION
1. Abstraction: The Vehicle class is abstract, which means it cannot be
instantiated directly. It defines the structure (with abstract and concrete
methods) that its subclasses must follow.
2. Abstract Method: The start method in the Vehicle class is abstract, so all
subclasses (Car and Motorcycle) must provide their implementation of this
method.
3. Polymorphism: The Vehicle reference type is used to hold objects of its
subclasses (Car and Motorcycle), demonstrating polymorphism.
This approach ensures that any class extending Vehicle must define its specific way
of starting, but they all inherit the stopping functionality.
// Performing transactions
account.deposit(500.0);
System.out.println("Balance after deposit: " + account.getBalance());
account.withdraw(300.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
OUTPUT
Account Holder: John Doe
Current Balance: 1000.0
Deposited: 500.0
Balance after deposit: 1500.0
Withdrawn: 300.0
Balance after withdrawal: 1200.0
Deposit amount must be positive.
Insufficient balance.
Updated Account Holder: Jane Doe
Key Features of Encapsulation in the Example:
1. Private Fields: The fields accountHolderName and balance are private,
ensuring they cannot be accessed directly.
2. Public Methods: Methods like getAccountHolderName(), getBalance(),
deposit(), and withdraw() provide controlled access to the fields.
3. Validation: Logic within methods ensures only valid data can be set or
modified (e.g., deposit and withdrawal checks).
4. Flexibility: The implementation details are hidden, and the interface for
interacting with the object is clean and straightforward.
This is a classic use of encapsulation to protect and control access to sensitive data.
// Parent class
class Animal {
// Method common to all animals
void eat() {
System.out.println("This animal eats food.");
}
}
OUTPUT
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
Explanation:
1. Inheritance: The Dog and Cat classes inherit from the Animal class. This
means they have access to the eat() method defined in the Animal class.
2. Specialization: The Dog and Cat classes add their specific behavior (bark()
and meow() methods) while still inheriting common behavior from the Animal
class.
3. Reusability: The common eat() method is written once in the Animal class
but can be used by all its subclasses.
This approach demonstrates how inheritance promotes code reuse and allows
extending the behavior of a parent class in child classes.
// Abstract superclass
abstract class Shape {
// Abstract method to calculate area
abstract double calculateArea();
// Constructor
public Circle(double radius) {
this.radius = radius;
}
// Implementing calculateArea
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
}
// Constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Implementing calculateArea
@Override
double calculateArea() {
return length * width;
}
}
// Constructor
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// Implementing calculateArea
@Override
double calculateArea() {
return 0.5 * base * height;
}
}
System.out.println("\nRectangle:");
shape2.displayArea();
System.out.println("\nTriangle:");
shape3.displayArea();
}
}
Circle:
Area: 78.53981633974483
Rectangle:
Area: 24.0
Triangle:
Area: 12.0
Explanation:
1. Abstract Class (Shape):
o Shape defines an abstract method calculateArea() that is overridden by
all subclasses.
o It also includes a concrete method displayArea() that uses the
calculateArea() method to print the area of the shape.
2. Subclasses (Circle, Rectangle, Triangle):
o Each subclass provides its specific implementation of the
calculateArea() method based on the shape's formula.
3. Polymorphism:
o A Shape reference (shape1, shape2, shape3) is used to hold objects of
its subclasses (Circle, Rectangle, Triangle).
o The method displayArea() is called on the Shape reference, and the
correct calculateArea() method is invoked dynamically at runtime
based on the actual object type.