0% found this document useful (0 votes)
4 views9 pages

Inheritance Lab

The document contains Java code demonstrating inheritance and interfaces through various classes such as Person, Employee, Order, ShippedOrder, PhoneBook, and Teacher. It illustrates the use of abstract classes, interfaces, and method overriding, along with examples of handling personal and business records in a phone book application. Additionally, it showcases a college system with teacher and department details, emphasizing object-oriented programming principles.

Uploaded by

Utkarsh Mishra
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)
4 views9 pages

Inheritance Lab

The document contains Java code demonstrating inheritance and interfaces through various classes such as Person, Employee, Order, ShippedOrder, PhoneBook, and Teacher. It illustrates the use of abstract classes, interfaces, and method overriding, along with examples of handling personal and business records in a phone book application. Additionally, it showcases a college system with teacher and department details, emphasizing object-oriented programming principles.

Uploaded by

Utkarsh Mishra
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

Inheritance lab

Q2:

Ans: a: public abstract class Person {

public void info() {

[Link]("This is a person.");

public abstract void getData();

b: public interface PasswordInterface {

int PASSWORD = 123;

void displayData();

C: public class Employee extends Person implements PasswordInterface {

private String name;

private int deptNumber;

private int code;

public Employee(String name, int deptNumber, int code) {

[Link] = name;

[Link] = deptNumber;

[Link] = code;

@Override

public void getData() {

[Link]("Getting employee data...");

[Link]("Name: " + name);

[Link]("Department Number: " + deptNumber);

[Link]("Code: " + code);

@Override

public void displayData() {

[Link]("Displaying employee data...");


[Link]("Name: " + name);

[Link]("Department Number: " + deptNumber);

[Link]("Code: " + code);

d: public final class MainClass {

public static void main(String[] args) {

Employee employee = new Employee("John Doe", 1234, 5678);

[Link]();

[Link]();

[Link]();

Q3:

Ans: [Link]

public class Order {

private String customerName;

private int customerNumber;

private double orderQuantity;

private double unitCost;

public String getCustomerName() {

return customerName;

public void setCustomerName(String customerName) {

[Link] = customerName;

public int getCustomerNumber() {

return customerNumber;

}
public void setCustomerNumber(int customerNumber) {

[Link] = customerNumber;

public double getOrderQuantity() {

return orderQuantity;

public void setOrderQuantity(double orderQuantity) {

[Link] = orderQuantity;

public double getUnitCost() {

return unitCost;

public void setUnitCost(double unitCost) {

[Link] = unitCost;

public double computePrice() {

return orderQuantity * unitCost;

[Link]
public class ShippedOrder extends Order {

private static final double SHIPPING_HANDLING_CHARGE = 40.0;

@Override

public double computePrice() {

return [Link]() + SHIPPING_HANDLING_CHARGE;

[Link]
public class UseOrder {
public static void main(String[] args) {

ShippedOrder order = new ShippedOrder();

[Link]("John Doe");

[Link](12345);

[Link](10);

[Link](50.0);

[Link]("Customer Name: " + [Link]());

[Link]("Customer Number: " + [Link]());

[Link]("Order Quantity: " + [Link]());

[Link]("Unit Cost: " + [Link]());

double totalCost = [Link]();

[Link]("Total Cost: " + totalCost);

Q4:

Ans: [Link]

import [Link];

public class PhoneBook {

private String name;

private String phone;

private String address;

public class Personal {

private String relation;

public void getInput() {

Scanner scanner = new Scanner([Link]);

[Link]("Enter relation: ");

relation = [Link]();

}
public void putInput() {

[Link]("Relation: " + relation);

public static class Business {

private String organization;

private String dept;

private String mobile;

public void accept() {

Scanner scanner = new Scanner([Link]);

[Link]("Enter organization: ");

organization = [Link]();

[Link]("Enter department: ");

dept = [Link]();

[Link]("Enter mobile: ");

mobile = [Link]();

public void show() {

[Link]("Organization: " + organization);

[Link]("Department: " + dept);

[Link]("Mobile: " + mobile);

[Link]
import [Link];
public class MainClass {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
PhoneBook phoneBook = new PhoneBook();

[Link]("Choose an option:");
[Link]("1. Enter Personal Record");
[Link]("2. Enter Business Record");
int choice = [Link]();
[Link]();

if (choice == 1) {
[Link] personalRecord = [Link] Personal();
[Link]("Enter name: ");
[Link] = [Link]();
[Link]("Enter phone: ");
[Link] = [Link]();
[Link]("Enter address: ");
[Link] = [Link]();

[Link]();
[Link]("\nPersonal Record:");
[Link]("Name: " + [Link]);
[Link]("Phone: " + [Link]);
[Link]("Address: " + [Link]);
[Link]();
} else if (choice == 2) {
[Link] businessRecord = new [Link]();
[Link]("Enter name: ");
[Link] = [Link]();
[Link]("Enter phone: ");
[Link] = [Link]();
[Link]("Enter address: ");
[Link] = [Link]();
[Link]();
[Link]("\nBusiness Record:");
[Link]("Name: " + [Link]);
[Link]("Phone: " + [Link]);
[Link]("Address: " + [Link]);
[Link]();
} else {
[Link]("Invalid choice.");
}
[Link]();
}
}

Q5:
Ans: [Link]

public class Teacher {


private String name;
private String qualification;
public Teacher(String name, String qualification) {
[Link] = name;
[Link] = qualification;
}
public String getName() {
return name;
}
public String getQualification() {
return qualification;
}
}

[Link]
public class Department extends Teacher {
private int deptNo;
private String deptName;
public Department(String name, String qualification, int deptNo, String deptName) {
super(name, qualification);
[Link] = deptNo;
[Link] = deptName;
}
public int getDeptNo() {
return deptNo;
}
public String getDeptName() {
return deptName;
}
}

[Link]
public interface College {
String COLLEGE_NAME = "ABC College of Engineering";
}
[Link]

import [Link];

public class MainClass implements College {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter Teacher's Name: ");

String name = [Link]();

[Link]("Enter Teacher's Qualification: ");

String qualification = [Link]();

[Link]("Enter Department Number: ");

int deptNo = [Link]();

[Link]();

[Link]("Enter Department Name: ");

String deptName = [Link]();

Department department = new Department(name, qualification, deptNo, deptName);

[Link]("\nCollege Name: " + COLLEGE_NAME);

[Link]("Teacher's Name: " + [Link]());

[Link]("Qualification: " + [Link]());

[Link]("Department Number: " + [Link]());

[Link]("Department Name: " + [Link]());

[Link]();

You might also like