In this lesson, you will learn
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).

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

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
complete notes with proper examples of code
You must be logged in to submit a review.