0% found this document useful (0 votes)
95 views4 pages

CDOP3203

A subclass inherits from a superclass. The subclass can override methods from the superclass that have the same name, number, and type of parameters. Private data in a class can be accessed in other classes through getter and setter methods. A Person class contains private data fields for name, address, phone, and email with getter and setter methods. Student and Employee classes extend Person with additional fields for student number, salary, and department ID. They call the Person constructor and display method while overriding display to show their own fields.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views4 pages

CDOP3203

A subclass inherits from a superclass. The subclass can override methods from the superclass that have the same name, number, and type of parameters. Private data in a class can be accessed in other classes through getter and setter methods. A Person class contains private data fields for name, address, phone, and email with getter and setter methods. Student and Employee classes extend Person with additional fields for student number, salary, and department ID. They call the Person constructor and display method while overriding display to show their own fields.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Part A

1. Inheritance
2. A "subclass" is created when it is derrived from "another class". The "class"
that it is derrived from is also known as "superclass". Its a hierarchy of objects
in which the "subclass" inherits from "superclass".
3. The correct method to access a private data from a different class is by
creating and using "getter" and "setter" methods. This is also called "accessor"
and "mutator" methods.
4. 5
5. "Method overriding" is an approach that replaces a relatively similar method
from the inherited class in a subclass. Usually, the "overriding method" have the
same name, number and types of parameter values, and return value type as the
inherited method.

Part B
1.

public class Account {

private int ID;


private double BALANCE;

public Account(int id, double balance ) {


ID = id;
BALANCE = balance;
}

public int getId() {


return ID;
}

public double getBalance () {


return BALANCE;
}

public void setId (int id) {


ID = id;
}

public void withdraw (double amount) {


if (amount >= BALANCE)
BALANCE -= amount;
}

public void deposit (double amount) {


BALANCE += amount;
}
}

2.

public class Person {

private String Name;


private String Address;
private String Phone;
private String Email;
public Person () {
Name = "";
Address = "";
Phone = "";
Email = "";
}

public Person (String name, String address, String phone, String email) {
Name = name;
Address = address;
Phone = phone;
Email = email;
}

public String getName() {


return Name;
}

public String getAddress() {


return Address;
}

public String getPhone() {


return Phone;
}

public String getEmail() {


return Email;
}

public void setName(String name) {


Name = name;
}

public void setAddress(String address) {


Address = address;
}

public void setPhone(String phone) {


Phone = phone;
}

public void setEmail(String email) {


Email = email;
}

public void display () {


System.out.println("Name :" + Name);
System.out.println("Address :" + Address);
System.out.println("Phone :" + Phone);
System.out.println("Email :" + Email);
}
}

public class Student extends Person {

private int StudentNo;

public Student () {
super();
StudentNo = "";
}

public Student (String name, String address, String phone, String email, int
studentno) {
super(name, address, phone, email);
StudentNo = studentno;
}

public int getStudentNo () {


return StudentNo;
}

public void setStudentNo (int studentno) {


StudentNo = studentno;
}

public void display () {


super.display();
System.out.println("StudentNo :" + StudentNo);
}

public class Employee extends Person {

private double Salary;


private int DepartmentId;

public Employee() {
super();
Salary = 0.00;
DepartmentId = 0;
}

public Employee(String name, String address, String phone, String email,


double salary, int departmentid) {
super(name, address, phone, email);
Salary = salary;
DepartmentId = departmentid;
}

public double getSalary() {


return Salary;
}

public int getDepartmentId() {


return DepartmentId;
}

public void setSalary() {


Salary = salary;
}

public void setDepartmentId() {


DepartmentId = departmentid;
}
public void display () {
super.display();
System.out.println("Salary :" + Salary);
System.out.println("DepartmentId :" + DepartmentId);
}
}

You might also like