0% found this document useful (0 votes)
9 views

Java Inheritance Example Assign Hira Khan

This file give you the important real life examples of Inheritance in java.

Uploaded by

My Choice
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)
9 views

Java Inheritance Example Assign Hira Khan

This file give you the important real life examples of Inheritance in java.

Uploaded by

My Choice
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/ 9

Example No.

1:

*1. Animal Hierarchy*

// Parent class

class Animal {

void sound() {

System.out.println("Makes sound");

//Child class

class Dog extends Animal {

void Dogsound() {

System.out.println("Barks");

class Cat extends Animal {

void Catsound() {

System.out.println("Meows");

```

Example No.2

*2. Shape Inheritance*

class Shape {

void draw() {

System.out.println("Drawing shape");

}
class Circle extends Shape {

void CircleDraw() {

System.out.println("Drawing circle");

class Rectangle extends Shape {

void RectangleDraw() {

System.out.println("Drawing rectangle");

```

*3. Vehicle Inheritance*

```

class Vehicle {

void move() {

System.out.println("Moving");

class Car extends Vehicle {

void Move() {

System.out.println("Driving");

class Bike extends Vehicle {

void Move() {
System.out.println("Riding");

```

Example 4

*4. Employee Hierarchy*

class Employee {

String name;

int salary;

Employee(String name, int salary) {

this.name = name;

this.salary = salary;

class Manager extends Employee {

Manager(String name, int salary) {

super(name, salary);

class Developer extends Employee {

Developer(String name, int salary) {

super(name, salary);

```

Example No.5
// Grandparent Example

class Grandparent {

void tradition() {

System.out.println("Following tradition");

class Parent extends Grandparent {

void culture() {

System.out.println("Following culture");

//Example No6

class Child extends Parent {

void language() {

System.out.println("Speaking language");

//Example No.7

```

class Vehicle {

void move() {

System.out.println("Moving");

class Car extends Vehicle {

void move() {

System.out.println("Driving");

}
class Truck extends Vehicle {

void move() {

System.out.println("Hauling");

```

*8. Example No.8*

class Printable {

void print() {

System.out.println("Printing document");

class Document extends Printable {

class Scanner extends Document {

void scan() {

System.out.println("Scanning");

//Example 9

class Person {

String name;
Person(String name) {

this.name = name;

class Employee extends Person {

int salary;

Employee(String name, int salary)

Person(name);

this.salary = salary;

//Example 10

//University Management System

// Parent class (Person)

class Person {

private String name;

private int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

public void displayInfo() {

System.out.println("Name: " + name);


System.out.println("Age: " + age);

// Child class (Student)

class Student extends Person {

private String rollNumber;

private String course;

public Student(String name, int age, String rollNumber, String course) {

super(name, age); // Calling Person constructor

this.rollNumber = rollNumber;

this.course = course;

public void displayStudentInfo() {

displayInfo(); // Calling Person method

System.out.println("Roll Number: " + rollNumber);

System.out.println("Course: " + course);

// Child class (Faculty)

class Faculty extends Person {

private String department;

private String designation;

public Faculty(String name, int age, String department, String designation) {

super(name, age); // Calling Person constructor

this.department = department;

this.designation = designation;
}

public void displayFacultyInfo() {

displayInfo(); // Calling Person method

System.out.println("Department: " + department);

System.out.println("Designation: " + designation);

public class University {

public static void main(String[] args) {

Student student = new Student("John Doe", 20, "S101", "Computer Science");

student.displayStudentInfo();

Faculty faculty = new Faculty("Jane Smith", 35, "Computer Science", "Professor");

faculty.displayFacultyInfo();

```

*Output:*

```

Name: John Doe

Age: 20

Roll Number: S101

Course: Computer Science

Name: Jane Smith

Age: 35
Department: Computer Science

Designation: Professor

You might also like