0% found this document useful (0 votes)
3 views

Inheritance

The document contains Java code examples demonstrating different types of inheritance: single, multilevel, and hierarchical. It also includes an interface implementation showcasing vehicle attributes and methods. Each section illustrates how classes can inherit properties and methods from a parent class and how interfaces can define behaviors for implementing classes.

Uploaded by

abc xyz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Inheritance

The document contains Java code examples demonstrating different types of inheritance: single, multilevel, and hierarchical. It also includes an interface implementation showcasing vehicle attributes and methods. Each section illustrates how classes can inherit properties and methods from a parent class and how interfaces can define behaviors for implementing classes.

Uploaded by

abc xyz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Chaitrali Inamdar

21143179

Single Inheritance
class Vehicle {

protected String brand = "Ford";

public void Mileage() {

System.out.println("21/23 KMPL");
}}

class Single_inheritance extends Vehicle {


private String modelName = "Mustang";

public static void main(String[] args) {

Single_inheritance myCar = new Single_inheritance();

myCar.Mileage();

System.out.println(myCar.brand + " " + myCar.modelName);


}

}
Multilevel Inheritance

class Vehicle {

protected String brand = "Ford";

protected void Mileage() {


System.out.println("21/23 KMPL");
}

}
class second_level extends Vehicle

{
public void Version()

{
Mileage();

System.out.println("1.2 Ti-VCT Ambiente (MT) Petrol");

}
}

class third_level extends second_level {

private String modelName = "Mustang";

public static void main(String[] args) {


second_level myCar = new second_level();
myCar.Version();

System.out.println(myCar.brand);
}

}
Hierarchical Inheritance

class Vehicle {

protected String brand = "Ford";

protected void Mileage() {


System.out.println("In Parent Class");
System.out.println("21/23 KMPL");

} protected void Color() {


System.out.println("In Parent Class");

System.out.println("Black");
}} class Child1 extends Vehicle

{ public void Version()


{ System.out.println("In Child1 Class");

Mileage();

System.out.println("1.2 Ti-VCT Ambiente (MT) Petrol");


}} class Child2 extends Vehicle

{ public void Fuel_type()

System.out.println("In child2 Class");


Color();
System.out.println("Petrol");

}}
class Hierarchical { private String modelName = "Mustang";

public static void main(String[] args) {


Child1 myCar = new Child1 ();

Child2 mycar2=new Child2();

myCar.Version();

mycar2.Fuel_type();
System.out.println(myCar.brand);
}}

Inetrface
interface Vehicle{
void print();

} class Interface_demo implements Vehicle{


public void print(){

System.out.println("Model name= Ford, Milege= 21/23 KMPL");

} } class Interface_demo2 implements Vehicle{

public void print(){

System.out.println("Color=black, version=1.2 Ti-VCT Ambiente (MT) Petrol");


}} class Inter

{public static void main(String args[]){


Interface_demo obj=new Interface_demo();

obj.print();
Interface_demo2 obj2=new Interface_demo2();

obj2.print(); } }

You might also like