[Go to site: main page, start]

0% found this document useful (0 votes)
7 views11 pages

Java Multilevel Inheritance Examples

The document provides examples of multilevel inheritance in Java, showcasing various classes and their relationships. It includes code snippets for different scenarios such as basic inheritance, method overriding, constructor chaining, and polymorphism. Each example is accompanied by a main method demonstrating the functionality of the classes involved.

Uploaded by

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

Java Multilevel Inheritance Examples

The document provides examples of multilevel inheritance in Java, showcasing various classes and their relationships. It includes code snippets for different scenarios such as basic inheritance, method overriding, constructor chaining, and polymorphism. Each example is accompanied by a main method demonstrating the functionality of the classes involved.

Uploaded by

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

Multilevel Inheritance in Java

Lab Programs with Code and Output


[Link]
java-compiler
Example 1: Basic Multilevel
Code:
Inheritance
class Grandparent {
void showGrandparent() {
[Link]("I am the Grandparent");
}
}
class Parent extends Grandparent {
void showParent() {
[Link]("I am the Parent");
}
}
class Child extends Parent {
void showChild() {
[Link]("I am the Child");
}
}
public class Test1 {
public static void main(String[] args) {
Child c = new Child();
[Link]();
[Link]();
[Link]();
}
}
Example 2: Inheriting Properties
Code:
class Animal {
void eat() { [Link]("Animals eat food"); }
}
class Mammal extends Animal {
void walk() { [Link]("Mammals walk on land"); }
}
class Dog extends Mammal {
void bark() { [Link]("Dog barks"); }
}
public class Test2 {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
[Link]();
}
}
Example 3: Inheritance with Fields
Code:
class Vehicle {
int wheels = 4;
}
class Car extends Vehicle {
String fuel = "Petrol";
}
class SportsCar extends Car {
int speed = 200;
}
public class Test3 {
public static void main(String[] args) {
SportsCar sc = new SportsCar();
[Link]("Wheels: " + [Link]);
[Link]("Fuel: " + [Link]);
[Link]("Speed: " + [Link] + " km/h");
}
}
Example 4: University Example
Code:
class University {
void universityName() { [Link]("XYZ University"); }
}
class Faculty extends University {
void facultyName() { [Link]("Faculty of Computer Science"); }
}
class Department extends Faculty {
void deptName() { [Link]("Department of Software Engineering"); }
}
public class Test4 {
public static void main(String[] args) {
Department d = new Department();
[Link]();
[Link]();
[Link]();
}
}
Example 5: Constructor Chaining

Code:
class A { A() { [Link]("Constructor of A"); } }
class B extends A { B() { [Link]("Constructor of B"); } }
class C extends B { C() { [Link]("Constructor of C"); } }
public class Test5 {
public static void main(String[] args) { C obj = new C(); }
}
Example 6: Method Overriding

Code:
class Animal {
void sound() { [Link]("Animal makes sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Dog barks"); }
}
class Puppy extends Dog {
void sound() { [Link]("Puppy yelps"); }
}
public class Test6 {
public static void main(String[] args) {
Puppy p = new Puppy();
[Link]();
}
}
Example 7: Super Keyword
Code:
class A {
void display() { [Link]("Display of A"); }
}
class B extends A {
void display() {
[Link]();
[Link]("Display of B");
}
}
class C extends B {
void display() {
[Link]();
[Link]("Display of C");
}
}
public class Test7 {
public static void main(String[] args) {
C obj = new C();
[Link]();
}
}
Example 8: Bank System
Code:
class Account {
void accountType() { [Link]("This is a Bank Account"); }
}
class SavingsAccount extends Account {
void savingsFeatures() { [Link]("Savings Account: Interest rate applied");
}
}
class StudentSavings extends SavingsAccount {
void specialOffer() { [Link]("Student Savings: Zero balance facility"); }
}
public class Test8 {
public static void main(String[] args) {
StudentSavings ss = new StudentSavings();
[Link]();
[Link]();
[Link]();
}
}
Example 9: Private + Getter
Code:
class Person {
private String name = "John";
String getName() { return name; }
}
class Employee extends Person {
String designation = "Software Engineer";
}
class Manager extends Employee {
double salary = 80000;
}
public class Test9 {
public static void main(String[] args) {
Manager m = new Manager();
[Link]("Name: " + [Link]());
[Link]("Designation: " + [Link]);
[Link]("Salary: " + [Link]);
}
}
Example 10: Polymorphism
Code:
class Shape {
void draw() { [Link]("Drawing Shape"); }
}
class Rectangle extends Shape {
void draw() { [Link]("Drawing Rectangle"); }
}
class Square extends Rectangle {
void draw() { [Link]("Drawing Square"); }
}
public class Test10 {
public static void main(String[] args) {
Shape s = new Square();
[Link]();
}
}

You might also like