Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Working with Single Level Inheritance in Java

 

In this lesson, you will learn

  • Single Level Inheritance
  • Examples

 

Single Level Inheritance

  • When a class (subclass or child class) inherits from only one superclass (parent class).
  • This model allows the subclass to acquire the properties and methods of the superclass, enabling code reuse and a hierarchical classification of classes.

Core Concept:

  • One Parent, One Child:
    • A subclass extends only one superclass.
    • This establishes a clear and straightforward hierarchical relationship.

How it Works:

  • The extends keyword is used to implement single inheritance.
  • The subclass inherits all accessible (non-private) members of the superclass.

 

SingleLevelInheritance

 

Example 1: Single Level Inheritance

package ch9.l3;

//Create a superclass.
class A {
	int i, j;
	
	void setij(int i,int j) {
		this.i=i;
		this.j=j;
	}
	
	void showij() {
		System.out.println("i and j: " + i + " " + j);
	}
}

//Create a subclass by extending class A.
class B extends A {
	int k;
	
	void setk(int k) {
		this.k=k;
	}
	
	void showk() {
		System.out.println("k: " + k);
	}
	
	void sum() {
		System.out.println("i+j+k: " + (i+j+k));
	}
}
	
public class SingleInheritance {

	public static void main(String[] args) {
		A objA=new A();
		B objB=new B();
		
		//The superclass may be used by itself.
		objA.setij(10, 20);
		System.out.println("Contents of Super Class A: ");
		objA.showij();
		System.out.println();
		
		/* The subclass can access to all public members of
		 its superclass. */
		objB.setij(5, 6);
		objB.setk(7);
		System.out.println("Contents of Sub Class B: ");
		objB.showij();
		objB.showk();
		System.out.println("Sum of i, j and k in Subclass Object:");
		objB.sum();
	}
}

Output

Contents of Super Class A: 
i and j: 10 20

Contents of Sub Class B: 
i and j: 5 6
k: 7
Sum of i, j and k in Subclass Object:
i+j+k: 18

 

Example 2: Vehicle and Car Class

package ch9.l3;

//Superclass
class Vehicle {
	protected String brand = "Ford"; // Vehicle attribute
	public void honk() { // Vehicle method
		System.out.println("Tuut, tuut!");
	}
}

//Subclass
class Car extends Vehicle {
	private String modelName = "Mustang"; // Car attribute
	public void display() {
		System.out.println("Brand: " + brand + ", Model Name: " + modelName);
	}
}

//Main class
public class VehicleMain {
	public static void main(String[] args) {
		Car myCar = new Car();
		myCar.honk();
		myCar.display();
	}
}

Output

Tuut, tuut!
Brand: Ford, Model Name: Mustang

 

Explanation

In Java, a protected member of a class is accessible:

    • Protected members are accessible within the same package.
    • They are also accessible within subclasses, even if the subclass is in a different package.

 

     

    Exercises

    Problem Statement:

    Create a Bank class with a protected attribute balance. Include a method showBalance() that prints the balance. Then, define a subclass SavingsAccount that inherits Bank and includes a methoddeposit(int amount) to add to the balance. Demonstrate depositing an amount and showing the balance.

    Solution

    class Bank {
        protected int balance;
    
        Bank() {
            balance = 0;
        }
    
        void showBalance() {
            System.out.println("Current Balance: $" + balance);
        }
    }
    
    class SavingsAccount extends Bank {
        void deposit(int amount) {
            balance += amount;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            SavingsAccount myAccount = new SavingsAccount();
            myAccount.deposit(500); // Deposit $500
            myAccount.showBalance(); // Prints "Current Balance: $500"
        }
    }
    

     

     


    End of the lesson….enjoy learning

     

     

    Student Ratings and Reviews

     

     

     

    There are no reviews yet. Be the first one to write one.

     

     

    Submit a Review