0% found this document useful (0 votes)
14 views2 pages

Java Lab Inheritance

The document provides Java code examples demonstrating various types of inheritance: single, multi-level, and hierarchical. In the single inheritance example, a 'Branch' class extends 'College' to access its properties. The multi-level example shows a 'section' class extending 'Branch', while the hierarchical example features two student classes extending a common 'college' class, showcasing method inheritance.

Uploaded by

satyasri
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)
14 views2 pages

Java Lab Inheritance

The document provides Java code examples demonstrating various types of inheritance: single, multi-level, and hierarchical. In the single inheritance example, a 'Branch' class extends 'College' to access its properties. The multi-level example shows a 'section' class extending 'Branch', while the hierarchical example features two student classes extending a common 'college' class, showcasing method inheritance.

Uploaded by

satyasri
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/ 2

7.

Write a Java program to implement various types of inheritance


i. Single ii. Multi-Level iii. Hierarchical iv. Hybrid
i. Single

class College{
String name="Bullayya College";
}
class Branch extends College{
int totalseats=1000;
public static void main(String[] args) {
Branch cs=new Branch();
System.out.println("College name: "+cs.name);
System.out.println("Total seats: "+cs.totalseats);
}
}

ii. Multi-Level
class College{
String name="Bullayya College";
}
class Branch extends College{
int totalseats=1000;
}
class section extends Branch
{
int sections=5;

public static void main(String[] args) {


section cs=new section();
System.out.println("College name: "+cs.name);
System.out.println("Total seats: "+cs.totalseats);
System.out.println("Total seats: "+cs.sections);
}
}
iii. Hierarchical
class college{
void cs1(){
System.out.println("studying c language...");
}
}
class student1 extends college {
void cs2()
{
System.out.println("studying datastructures language...");
}
}
class student2 extends college {
void cs5()
{
System.out.println("explaing java");
}
}
class faculty
{
public static void main(String args[])
{
student1 c=new student1();
c.cs1();
c.cs2();

}
}

You might also like