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

Java Inheritance and Polymorphism Examples

The document contains Java programs demonstrating inheritance and polymorphism, specifically addressing the concept that Java does not support multiple inheritance but can achieve it through interfaces. It includes code examples for single inheritance with a Teacher and Student class, and multilevel inheritance with Faculty, Learner, and Monitor classes. Each program is accompanied by a brief output section attributed to Avinash Mishra.

Uploaded by

avikation1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Java Inheritance and Polymorphism Examples

The document contains Java programs demonstrating inheritance and polymorphism, specifically addressing the concept that Java does not support multiple inheritance but can achieve it through interfaces. It includes code examples for single inheritance with a Teacher and Student class, and multilevel inheritance with Faculty, Learner, and Monitor classes. Each program is accompanied by a brief output section attributed to Avinash Mishra.

Uploaded by

avikation1234
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT-4

Create Java programs using inheritance and polymorphism


a. “Java does not support multiple inheritance but we can achieve it by
interface”. Write a program to justify the above statement.
CODE:
package javalab;

interface A {
int a = 10;
void show();
}
interface B {
int b = 20;
void show();
}
class C implements A, B {
int c;
public void show() {
c = A.a + B.b;
[Link]("A.a = " + A.a);
[Link]("B.b = " + B.b);
[Link]("C.c = " + c);
}
}
public class Interface {
public static void main(String[] args) {
C obj = new C();
[Link]();
}
}

OUTPUT:

Avinash Mishra
2300321540055
b. Write a program in java to implement the following types of inheritance:
 Single Inheritance
 Multilevel Inheritance

CODE: // SINGLE INHERITANCE
package javalab;
class Teacher {
void teach() {
[Link]("Teaching students");
}
}
class Student extends Teacher {
void study() {
[Link]("Studying subjects");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Student s = new Student();
[Link]();
[Link]();
}
}
OUTPUT:

Avinash Mishra
2300321540055
CODE: // MULTILEVEL INHERITANCE
package javalab;
class Faculty {
void teach() {
[Link]("Teaching students");
}
}
class Learner extends Faculty {
void study() {
[Link]("Studying subjects");
}
}
class Monitor extends Learner {
void monitorClass() {
[Link]("Monitoring the class");
}
}
public class MultilevelInheritance{
public static void main(String[] args) {
Monitor m = new Monitor();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:

Avinash Mishra
2300321540055

You might also like