Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Understanding Hybrid Inheritance in Java

In this lesson, you will learn

  • Hybrid Inheritance

 

Hybrid Inheritance

Hybrid inheritance in Java is a combination of more than one type of inheritance.

It could involve a mix of single inheritance, multilevel inheritance, hierarchical inheritance, and/or multiple inheritance (though Java itself doesn’t directly support multiple inheritance with classes, it achieves a similar effect through interfaces).

 

Example-1: Hybrid Inheritance

package ch10.l7;

interface Vehicle {
    void vehicleType();
}

class TwoWheeler implements Vehicle {
    public void vehicleType() {
        System.out.println("Vehicle Type: Two Wheeler");
    }
}

class FourWheeler implements Vehicle {
    public void vehicleType() {
        System.out.println("Vehicle Type: Four Wheeler");
    }
}

class Bike extends TwoWheeler {
    void bikeType() {
        System.out.println("Bike Type: Sports Bike");
    }
}

class Car extends FourWheeler {
    void carType() {
        System.out.println("Car Type: Sedan");
    }
}

public class HybridInheritanceDemo {
    public static void main(String[] args) {
        Bike bike = new Bike();
        bike.vehicleType();
        bike.bikeType();

        Car car = new Car();
        car.vehicleType();
        car.carType();
    }
}

 

Output

Vehicle Type: Two Wheeler
Bike Type: Sports Bike
Vehicle Type: Four Wheeler
Car Type: Sedan

 

Example-2: Hybrid Inheritance

 

package ch10.l7;

class Student{
	int rollnumber;
	
	void getRollNumber(int rn) {
		rollnumber=rn;
	}
	void showRollNumber() {
		System.out.println("Rollnumber="+rollnumber);
	}
}

class Test extends Student{
	float part1, part2;
	void getMarks(float m1, float m2) {
		part1=m1;
		part2=m2;
	}
	void showMarks() {
		System.out.println("Marks Obtained: ");
		System.out.println("Part1: "+part1);
		System.out.println("Part2: "+part2);
	}
}

interface Sports{
	float sportsweight=60.0f;
	void putweigth();
}

class Result extends Test implements Sports{
	float total;

	@Override
	public void putweigth() {
		System.out.println("Sports Weigth: "+sportsweight);	
	}
	void display() {
		total=part1+part2+sportsweight;
		showRollNumber();
		showMarks();
		putweigth();
		System.out.println("Total Score: "+total);
	}
}

public class Hybrid {

	public static void main(String[] args) {
		Result student1=new Result();
		student1.getRollNumber(1001);
		student1.getMarks(78.5f, 84.5f);
		student1.display();
	}
}

 

Output

Rollnumber=1001
Marks Obtained: 
Part1: 78.5
Part2: 84.5
Sports Weigth: 60.0
Total Score: 223.0

 

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

24/03/2025

complete notes with proper examples of code

 

 

Submit a Review