4 principles of OOPs Inheritance--general-->more specific--code reuse Encapsulation--seperation of concerns--every class should be self contained, as few dependencies as possible Polymorphism--adapt and behave differently depending on environment --overloading methods, overriding methods--generics Abstraction--generalize, group everthing that is common 4 Types of Relationships Inheritance Association Composition Aggregation
Here is the diagram that shows the inheritance hierarchy
Here are the code classes that show that inheritance
Person Class
package com.spconger; public abstract class Person { private String number; private String name; private String email; private String phone; public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
The Customer Class
package com.spconger; import java.util.ArrayList; public class Customer extends Person{ ArrayList<String> coupons = new ArrayList<String>(); public void AddCoupons(String coupon){ coupons.add(coupon); } public String toString(){ return getName(); } }
The EmployeeClass
package com.spconger; public abstract class Employee extends Person { private String position; private String hireDate; public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public String getHireDate() { return hireDate; } public void setHireDate(String hireDate) { this.hireDate = hireDate; } public double calculatePay(){ return 0.0; } }
The SalaryEmployee Class
package com.spconger; public class SalaryEmployee extends Employee{ private double salary; public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double calculatePay(){ return getSalary()/12; } }
Program Class
package com.spconger; import java.util.Scanner; public class Program { public static void main(String[] args) { // TODO Auto-generated method stub Customer c = new Customer(); c.setName("George"); SalaryEmployee se = new SalaryEmployee(); se.setName("Jenny"); se.setSalary(30000.00); System.out.println(se.getName() + "," + se.getSalary() + ", " + se.calculatePay()); System.out.println(c.toString()); } }
Composition and Aggregation Diagrams
No comments:
Post a Comment