Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

CHAPTER-SEVEN

OBJECT-ORIENTED
IMPLEMENTATION

1
Object Oriented Programming
Example
• A Company has a list of Employees. It asks you
to provide a payroll sheet for all employees.
– Has extensive data (name, department, pay
amount, …) for all employees.
– Different types of employees – manager,
engineer, software engineer.
– You have an old Employee class but need to
add very different data and methods for
managers and engineers.
• Suppose someone wrote a name system, and
already provided a legacy Employee class. The
old Employee class had a printData() method for
each Employee that only printed the name. We
REVIEW PICTURE
Encapsulation Message passing "Main event loop"

Employee e1 public … Main(…){


printData Employee e1…(“Abebe",“Kebede");
...
private:
e1.printData();
lastName // Prints Employee names.
firstName
...
}
Employee class
This is a simple super or base class.
class Employee {

}
Inheritance
Already written:
Class Employee
firstName printData()
lastName

is-a is-a
Class Engineer
Class Manager
firstName firstName
lastName lastName

hoursWorked
salary
printData() wages
getPay()
printData()
getPay()
You next write:
Engineer class
Subclass or (directly) derived class
class Engineer extends Employee {

Manager class
Subclass or (directly) derived class

class Manager extends Employee {

}
Inheritance…
Class Manager
firstName
lastName

is-a
Salary
printData
getPay
Class SalesManager
firstName
A SalesManager lastName
gets a constant
salary of 1250.0 Salary
printData
getPay salesBonus
SalesManager Class
(Derived class from derived class)
class SalesManager extends Manager {

}
Main method
public class PayRoll {
public static void main(String[] args) {

}
}

Output from main method


Abe Sol
Weekly payment is: 320.32
Kebede Girma
Monthly salary is: 4500.0
Selamawit Eshetu
Monthly salary: 1250.0
Bonus is: 1000.0
Class Employee
import java.io.*;
class Employee {
private String firstName;
private String lastName;
public Employee(String fName, String lName)
{
firstName=fName;
lastName=lName;
}
public void printData() {
System.out.println(firstName+ " "
+lastName);
}
}
Class Engineer
class Engineer extends Employee {
private double wage;
private double hoursWorked;
public Engineer(String fName, String lName,
double rate, double hours) {
super(fName, lName);
wage = rate;
hoursWorked=hours;
}
public double getPay() {
return wage * hoursWorked;
}
public void printData() {
super.printData();
System.out.println("Weekly payment is:" +
getPay());}}
Class Manager
class Manager extends Employee {
private double salary;
public Manager(String fName, String lName, double
sal) {
super(fName, lName);
salary=sal;
}
public double getPay() {
return salary;
}
public void printData() {
super.printData();
System.out.println("Monthly salary is:"+ " "
+ getPay());
}
}
Class Sales Manager
class SalesManager extends Manager {
private double bonus;
public SalesManager(String fName, String lName,
double b) {
super(fName, lName, 1250.0);
bonus=b;
}
public double getPay() {
return 1250.0;
}
public void printData() {
super.printData();
System.out.println("Bonus is:"+ " " + bonus);
}
}
Class Payroll
public class PayRoll {
public static void main(String[] a) {
//Engineer abe=new Engineer("Abebe", "Solomon", 10.4,
30.8);
//Manager kebe= new Manager("Kebede",
"Girma",4500.0);
//SalesManager selam=new SalesManager("Selamawit",
"Eshetu", 1000.00);
Employee employees[]=new Employee[3];
employees[0]=new Engineer("Abebe", "Solomon", 10.4,
30.8);
employees[1]=new Manager("Kebede", "Girma",4500.0);;
employees[2]=new SalesManager("Selamawit", "Eshetu",
1000.00);
for(int i=0; i<employees.length; i++)
{
employees[i].printData();
}}}
Main method
public class PayRoll {
public static void main(String[] args) {

}
}

Output from main method


Abe Sol
Weekly payment is: 320.32
Kebede Girma
Monthly salary is: 4500.0
Selamawit Eshetu
Monthly salary: 1250.0
Bonus is: 1000.0
Student Specification
• Create a class called Student, which should have two
properties, a name and a year, and methods to get the name
and get the year of the student. Initialize these properties to
arguments passed into the constructor.
• Create a subclass of Student called Undergrad. The
Undergrad constructor should accept name and year
arguments.
• Add a method to Undergrad called description which returns
a String containing the name of the undergrad, then a space,
then a capital 'U', then a space, and then the year of the
undergrad. For example, the description method of an
Undergrad instance with the name "Solomon" and the year
2006, should return the String "Solomon U 2006".
Student Specification…
• Create a subclass of Student called Grad. The Grad
constructor should accept name and year arguments. Add a
description method to Grad which returns a String containing
the name of the Grad, followed by a space and then the
letter 'G'. The description method of a Grad named "Hana"
should return the String "Hana G 2005".
• Create a subclass of Grad called ResearchAssistant.
ResearchAssistant has a salary that is initialized in the
constructor and a getPay method that returns the salary.
• Add a description method to ResearchAssistant which
returns a String containing the result of Grad's description
method, followed by salary. The description method of a
ResearchAssistant with the name "Abebe" and a 2000.00
salary would return the String "Abebe G 2000.0".
Student Specification…

• Create a class called StudentTest that has a


main method. Use the main method to test the
class hierarchy you just built. Create some
instances of Undergrad, Grad, and Research
Assistant. Print out the result of their description
methods. Compile and run.
Class Student
import java.io.*;
class Student{
private String name;
private double year;
public Student(String n, double y)
{
name=n;
year=y;
}
public String getName()
{
return name;
}
public double getYear()
{
return year;
} }
Class Undergraduate Student

class UnderGrad extends Student{

public UnderGrad(String n, double y)


{
super(n,y);
}

public String description()


{
return getName()+" " + "U"+" " +getYear();
}
}
Class Graduate Student

class Grad extends Student{

public Grad(String na, double ye)


{
super(na,ye);

public String description()


{
return getName()+" "+ "G"+" "+ getYear();
}
}
Class Research Assistant

class ResearchAssistant extends Grad{


double salary;
public ResearchAssistant(String n, double y, double s)
{
super(n,y);
salary=s;
}
public double getSalary()
{
return salary;
}
public String description()
{
return super.description()+" "+ " "+getSalary();
}}
Class Student Test

class StudentTest{
public static void main(String[] a){
UnderGrad ug=new UnderGrad("Abebe", 2003);
System.out.println(ug.description());
Grad g=new Grad("Kebede",2005);
System.out.println(g.description());
ResearchAssistant ra=new
ResearchAssistant("Kebede",2004,2000);

System.out.println(ra.description());
}
}

You might also like