Title: ObjectOriented Programming Part II
1Object-Oriented ProgrammingPart II
- COMP114
- Thursday January 31
2Announcements
- Read Section 4.2
- Study for test (Thursday, Feb. 7)
- All material up to today
- Book Ch. 1, 2.1-2.4, 3.1, 3.2, 3.4, 3.5, 4.1
- Extra office hours
- Tuesday 2/5 400 500
- Ill be out of office on 2/6-7
- TA office hours on web page
3Topics
- Last Time
- Review of References
- Object-Oriented Programming
- Encapsulation
- This Class
- Little more on Overloading
- Inheritance
- Protection
4Overloading
- Cant overload by return type in Java
- Example
- Compiler will complain.
int getLength( ) ... double getLength( ) ...
5Inheritance
- Idea is to extend functionality of an object
- Specialize
- Car specialized from Vehicle
- Toyota specialized from Car
- Each level of specialization typically adds new
functionality
6Inheritance
- Derive new class from an existing class.
Is-A Relationship
7Nomenclature
- Superclass
- Subclass
- Parent class
- Child class
- Base class
- Derived class
8Superclass Person
- public class Person
- public Person(String theName)
-
- name theName
-
- private String name null
9Subclass Employee
- public class Employee extends Person
-
- public void setSalary(double sal)
- salary sal
-
- public double getSalary()
- return salary
-
- // Add a salary
- private double salary 0
10Why Not Just Copy/Paste?
- If you need to make change, you have to change
all - You can't work with any of the objects as if it
were the base class (Person) - Example say you just need to send a security
email. Does it matter whether it's Student,
Staff, or Faculty?
11Derived Class
- Can add methods
- Can add new instance variables
- Can also override methods
- Cannot remove methods or instance variables
12Employee New Constructor
- public class Employee extends Person
-
- Employee(String theName, double sal)
-
- super(theName)
- salary sal
-
- private double salary 0
Constructor of superclass
13Rules for Contructors
- If no constructor specified
- Automatically constructed with class variables
set to 0 or null - If constructor specified
- If super() is not first command,
- then constructor of superclass automatically
called with no arguments
14Remember Superclass
- public class Person
- public Person(String theName)
-
- name theName
-
15Subclass Overrides Constructor
Overrides method in class Person
- public class Employee extends Person
-
- Employee(String theName)
-
- super(theName)
- employeeID EmployeeIDGen.newID()
-
- Employee(String theName, int id)
-
- super(theName)
- employeeID id
-
- private int employeeID 0
-
Not just for constructor
16Access
- public class Person
- public Person(String theName)
-
- name theName
-
- private String name null
Subclasses cant modify directly
17Why Not Allow Subclass Access
- Wed be back to same problem
- If we changed variable in superclass, wed have
to find (and change) all the places it was
manipulated.
18Private Methods not Inherited
- If a class has a private method, its meant to be
used locally - As a helper for something
- Not accessible from subclass.
19Dynamic Binding
- A reference variable of the superclass can also
reference subclasses -
Person p p new Employee(Lastra, 1234)
20Example From Book
- Person p new Person 4
- p0 new Person( "joe", 25, "New York",
"212-555-1212" ) - p1 new Student( "becky", 27, "Chicago",
"312-555-1212", 4.0 ) - p3 new Employee( "bob", 29, "Boston",
"617-555-1212", 100000.0 )
21Polymorphism
- From Websters
- the quality or state of being able to assume
different forms - Reference variable that is polymorphic can
reference objects of several types
22Parameters
- Very similar when object is used as parameter
In class bookStore public void debit(Person p,
double c) Student s
new Student(John Doe) bookStore.debit(s,
123.45)
23Multiple Types
- An object is in some sense
- of its own type and
- also of its ancestors types.
- Is there an Adam/Eve?
24Object Class
- Object is the top of the inheritance hierarchy.
- If class doesnt specify inheritance, its a
subclass of Object - So youve all been using OOP anyway
- Object has some useful methods.
- More on that later.
25Inheritance Hierarchies
- Inheritance proceeds along generations
- So
- Faculty, which is derived from Employee, which is
derived from Person - Can use the Person methods
26Has-A
- I said that inheritance exhibits the Is-A
relationship - Student Is-A Person
- Not a Has-A relationship
- Person Has-A address
- Has-A should be implemented using class
variables, not inheritance
27Summary
- Derived class extends base class
- Can add data and methods
- Can override superclass methods
- If not overridden, methods are inherited
28Next Time
- More on Objects
- Abstract methods
- Review for test