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

Hierarchical Inheritance in Java Examples

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)
35 views11 pages

Hierarchical Inheritance in Java Examples

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

Lab 10

Hierarchal Inheritance
Example:
CODE:
class Animal{
void eat(){
[Link]("Eating..."); }
}
class Dog extends Animal{
void bark(){
[Link]("Barking..."); }
}
class Cat extends Animal{
void meow(){
[Link]("Meowing..."); }
}
public class Main{
public static void main(String[] args) {
Cat c = new Cat();
[Link]();
[Link]();
}
}
Output:
Lab 10

Lab Task
Task: Create a class name shape with a method to print message (is
shape)
Then create two other classes name rectangle and circle.
Inherit the shape class both having a method to print it's respective class
shape method for example if you are creating an object of rectangle class
and calling the method to print with respect to rectangle object the
method should come on screen as rectangle as a shape you are bound to
only print rectangle or circle in the respective class while is shape must
be call from the parent class. now create a sub class square of rectangle
class having a method to print square is a rectangle shape.
Here is a shape must be call from the parent class. Finally create the
objects of all four classes to display the Hierarchical structure of
inheritance.
CODE:
class Shape {
void isShape() {
[Link]("This is a shape."); }
}
class Rectangle extends Shape {
void printRectangle() {
[Link]("Rectangle is a shape."); }
}
class Circle extends Shape {
void printCircle() {
[Link]("Circle is a shape."); }
}
class Square extends Rectangle {
void printSquare() {
[Link]("Square is a rectangle shape."); }
}
public class TestShapes {
public static void main(String[] args) {
Lab 10

Shape shape = new Shape();


[Link]();

Rectangle rectangle = new Rectangle();


[Link]();
[Link]();

Circle circle = new Circle();


[Link]();
[Link]();

Square square = new Square();


[Link]();
[Link]();
[Link]();
}
}
Output:

Task: Introduce a class called employee having private data as (code and
name). Create a get and set method for I/o for the following class
properties . Now create a sub class for employee called manager having
its own data ( salary, experience) also create get and set method for this
class I/O
Lab 10

Create another sub class of manager class called scientist having its own
data ( number of publication s) also make method get and set.
Write another subclass called of employee class called worker having its
own features as (number of hours, hourly wage). Create a overridden
method of all classes and create main class and create objects of
manager, worker and scientist.
CODE:
class Employee {
private int code;
private String name;

public void setCode(int code) {


[Link] = code;
}
public int getCode() {
return code;
}
public void setName(String name) {
[Link] = name;
}
public String getName() {
return name;
}
public void display() {
[Link]("Employee Code: " + code);
[Link]("Employee Name: " + name);
}
}
class Manager extends Employee {
private double salary;
private int experience;

public void setSalary(double salary) {


[Link] = salary;
}
public double getSalary() {
Lab 10

return salary;
}
public void setExperience(int experience) {
[Link] = experience;
}
public int getExperience() {
return experience;
}
public void display() {
[Link]();
[Link]("Salary: $" + salary);
[Link]("Experience: " + experience + " years");
}
}
class Scientist extends Manager {
private int publications;
public void setPublications(int publications) {
[Link] = publications;
}
public int getPublications() {
return publications;
}
public void display() {
[Link]();
[Link]("Publications: " + publications);
}
}
class Worker extends Employee {
private int hours;
private double hourlyWage;

public void setHours(int hours) {


[Link] = hours;
}
public int getHours() {
return hours;
}
Lab 10

public void setHourlyWage(double wage) {


[Link] = wage;
}
public double getHourlyWage() {
return hourlyWage;
}
public void display() {
[Link]();
[Link]("Working Hours: " + hours);
[Link]("Hourly Wage: $" + hourlyWage);
[Link]("Total Salary: $" + (hours * hourlyWage));}
}
public class TestEmployeeHierarchy {
public static void main(String[] args) {
Manager m = new Manager();
[Link](101);
[Link]("Ali");
[Link](80000);
[Link](5);
[Link]("=== Manager Information ===");
[Link]();

Scientist s = new Scientist();


[Link](102);
[Link]("Dr. Sara");
[Link](120000);
[Link](10);
[Link](25);
[Link]("\n=== Scientist Information ===");
[Link]();

Worker w = new Worker();


[Link](103);
[Link]("Ahmed");
[Link](40);
[Link](500);
[Link]("\n=== Worker Information ===");
Lab 10

[Link]();
}
}
Output:

Task: To Develop a reservation system for an airline using inheritance


consisting of 4classes namely flight data, business, economy & employs
flight data class need to have flight code source, destination , & data of
travel business class need all information as flight data class &
additionally contain passenger name, address, telephone,& amount
economy class contain all data as flight data and business class but
additionally contain discount. Employee class contain all data as
flightdata class but additionally contain employee code all classes must
have overriding get() & show () method to perform i/o finally write main ()
class & create objects of business , employee class & perform i/o
Lab 10

CODE:
import [Link];

class FlightData {
protected String flightCode;
protected String source;
protected String destination;
protected String dateOfTravel;

public void get(Scanner sc) {


[Link]("Enter Flight Code: ");
flightCode = [Link]();
[Link]("Enter Source: ");
source = [Link]();
[Link]("Enter Destination: ");
destination = [Link]();
[Link]("Enter Date of Travel (DD-MM-YYYY): ");
dateOfTravel = [Link]();
}

public void show() {


[Link]("Flight Code: " + flightCode);
[Link]("Source: " + source);
[Link]("Destination: " + destination);
[Link]("Date of Travel: " + dateOfTravel);
}
}
class Business extends FlightData {
protected String passengerName;
protected String address;
protected String telephone;
protected double amount;

public void get(Scanner sc) {


[Link](sc);
[Link]("Enter Passenger Name: ");
passengerName = [Link]();
Lab 10

[Link]("Enter Address: ");


address = [Link]();
[Link]("Enter Telephone: ");
telephone = [Link]();
[Link]("Enter Ticket Amount: ");
amount = [Link]();
[Link](); // consume newline
}
public void show() {
[Link]();
[Link]("Passenger Name: " + passengerName);
[Link]("Address: " + address);
[Link]("Telephone: " + telephone);
[Link]("Amount Paid: $" + amount);
}
}

class Economy extends Business {


private double discount;

public void get(Scanner sc) {


[Link](sc);
[Link]("Enter Discount (%): ");
discount = [Link]();
[Link](); // consume newline
}
public void show() {
[Link]();
[Link]("Discount: " + discount + "%");
}
}

class Employee extends FlightData {


private String employeeCode;

public void get(Scanner sc) {


[Link](sc);
Lab 10

[Link]("Enter Employee Code: ");


employeeCode = [Link]();
}
public void show() {
[Link]();
[Link]("Employee Code: " + employeeCode);
}
}

public class TestReservationSystem {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("=== Business Class Reservation ===");


Business businessPassenger = new Business();
[Link](sc);

[Link]("\n=== Employee Flight Entry ===");


Employee employee = new Employee();
[Link](sc);

[Link]("\n--- Business Class Passenger Details ---");


[Link]();

[Link]("\n--- Employee Flight Details ---");


[Link]();

[Link]();
}
}
Output:
Lab 10

You might also like