Java Inheritance
Java Inheritance
Lab-09
Lab Objectives:
1. Understanding the concept of inheritance
2. Implementation of Inheritance in java
Software Required:
Netbeans IDE
Inheritance in Java
class SimpleInheritance {
public static void main(String args []) {
A superOb = new A();
B subOb = new B();
These types of inheritance allow for code reuse and the creation of
hierarchical relationships between classes.
class DefineClassHierarchy
{ public static void main(String s[])
{
C c = new C();
M m = new M();
K k;
O o = new C();
H h = new H();
k = o;
m = h;
c = m;
System.out.println("All compilation errors resolved");
}
ANS: To define the required classes so that the program compiles without any errors, we need to
create the classes C, M, K, O, and H. Based on the provided code snippet, here's how we can
define these classes:
java
class C {}
class M {}
class K {}
class O extends C {}
class H extends M {}
With these class definitions, the program should compile without any errors. Each class has been
defined as per the requirements mentioned in the code snippet, and the assignments k = o, m = h,
and c = m are valid since the types of variables k, m, and c match the types of objects being
assigned to them.
THE END