[go: up one dir, main page]

0% found this document useful (0 votes)
18 views3 pages

Inheritance C++

This document contains examples of three types of inheritance in Java: 1) Single inheritance defines a Bike class with a wheel property and extends it to a Type class that can display wheel number. 2) Multilevel inheritance defines classes for animals, dogs, and colored dogs that inherit properties and methods to describe legs, breed, and color. 3) Hierarchical inheritance defines a base A class with a method and extends it to B, C, and D classes that can all call the A class method.

Uploaded by

Shrirang Patil
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)
18 views3 pages

Inheritance C++

This document contains examples of three types of inheritance in Java: 1) Single inheritance defines a Bike class with a wheel property and extends it to a Type class that can display wheel number. 2) Multilevel inheritance defines classes for animals, dogs, and colored dogs that inherit properties and methods to describe legs, breed, and color. 3) Hierarchical inheritance defines a base A class with a method and extends it to B, C, and D classes that can all call the A class method.

Uploaded by

Shrirang Patil
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/ 3

Single Inheritance

class Bike
{
public int wheels;
public void wheel(int w)
{
wheels=w;
}
}
class Type extends Bike
{
void show()
{
System.out.println("No of Wheels:- "+wheels);
}
}
class singleInheritance
{
public static void main(String args[])
{
Type t1=new Type();
t1.wheel(4);
t1.show();
}
Multilevel Inheritance:
class animal
{
public void leg(int legs)
{
System.out.println("\nNo of legs:- "+legs);
}
}
class dog extends animal
{
public void breed(String type)
{
System.out.println("\nBreed Type:- "+type);
}
}
class colour extends dog
{
public void color(String color)
{
System.out.println("\nColor :- "+color);
}
}
class Multilevel {
public static void main(String args[])
{
colour c1=new colour();
c1.leg(4);
c1.breed("Labrador");
c1.color("Black");
}
}
Heirarchical Inheritance:
class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class heirarchical
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}

You might also like