0% found this document useful (0 votes)
10 views4 pages

Akrb 1

Inheritance in programming allows a subclass to acquire properties and behaviors from a superclass, establishing an 'is-a' relationship. The document provides a code example demonstrating a Vehicle superclass and a Car subclass, highlighting the benefits of code reusability and logical structure, as well as the drawbacks like tight coupling and limited flexibility. Additionally, it discusses real-world applications of inheritance in banking systems, showcasing how different account types can be modeled.

Uploaded by

rvs1024p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Akrb 1

Inheritance in programming allows a subclass to acquire properties and behaviors from a superclass, establishing an 'is-a' relationship. The document provides a code example demonstrating a Vehicle superclass and a Car subclass, highlighting the benefits of code reusability and logical structure, as well as the drawbacks like tight coupling and limited flexibility. Additionally, it discusses real-world applications of inheritance in banking systems, showcasing how different account types can be modeled.

Uploaded by

rvs1024p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Definition

Inheritance allows a class (called the "subclass" or "child class") to acquire the properties and
behaviors of another class (called the "superclass" or "parent class"). This relationship is often
described as an "is-a" relationship, where the subclass "is a type of" the superclass.

SYNTAX:

class ParentClass {

// fields and methods

class ChildClass extends ParentClass {

// additional fields and methods

CODE:

// Superclass

class Vehicle {

protected int speed;

protected int fuel;

public Vehicle(int speed, int fuel) {

this.speed = speed;

this.fuel = fuel;

public void displayInfo() {

System.out.println("Speed: " + speed + " km/h");

System.out.println("Fuel: " + fuel + " liters");

// Subclass
class Car extends Vehicle {

private String brand;

public Car(int speed, int fuel, String brand) {

super(speed, fuel); // Calls the constructor of the superclass

this.brand = brand;

public void displayCarInfo() {

displayInfo(); // Calls the method of the superclass

System.out.println("Brand: " + brand);

// Main class to test inheritance

public class Main {

public static void main(String[] args) {

Car myCar = new Car(150, 50, "Toyota");

myCar.displayCarInfo();

}
Explanation of the Code

1. Superclass (Vehicle): This class has two fields, speed and fuel, and a method displayInfo() that prints
these attributes. The protected keyword allows subclasses to access these fields directly.

2. Subclass (Car): The Car class extends Vehicle, inheriting its fields and methods. Car has an additional
field brand and a method displayCarInfo() that first calls displayInfo() from the superclass to print
speed and fuel, then prints the car brand.

3. Main Class: We create an instance of Car and call its displayCarInfo() method, which outputs both the
vehicle's basic information and the car-specific brand.

OUT PUT:

Speed: 150 km/h

Fuel: 50 liters

Brand: Toyota
Pros of Inheritance

1. Code Reusability: Common functionality can be defined once in the superclass and reused by subclasses.

2. Simplifies Code: Reduces code duplication, making programs shorter and easier to manage.

3. Logical Structure: Encourages a hierarchical structure, which helps organize related classes and makes the design
more intuitive.

4. Extendability: New functionality can be added to existing code by creating subclasses, promoting scalability.

Cons of Inheritance

1. Tight Coupling: Subclasses are dependent on the superclass, making it difficult to make changes in the superclass
without affecting subclasses.

2. Increased Complexity: Deep inheritance hierarchies can make the code hard to follow and understand.

3. Limited Flexibility: A subclass can only inherit from one superclass in Java (single inheritance), which can be
restrictive.

4. Potential for Misuse: If inheritance is used where it isn’t appropriate (like when the "is-a" relationship doesn’t
hold), it can lead to code that is difficult to maintain or understand.

BLOCK DIAGRAM:
REAL TIME:

Banking and Finance Applications

In banking systems, inheritance is often used to model different types of bank accounts. Here’s a
hierarchy you might see:

 Superclass: Account

o Common attributes: accountNumber, balance, ownerName.

o Common methods: deposit(), withdraw(), getBalance().

 Subclasses: SavingsAccount, CheckingAccount, FixedDepositAccount

o SavingsAccount: May have additional methods to calculate interest.

o CheckingAccount: Might include methods for overdraft protection.

o FixedDepositAccount: Could add restrictions on withdrawals.

You might also like