ObjectOriented Programming Part II - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

ObjectOriented Programming Part II

Description:

You can't work with any of the objects as if it were the base class (Person) ... the quality or state of being able to assume different forms ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 29
Provided by: anselmo9
Category:

less

Transcript and Presenter's Notes

Title: ObjectOriented Programming Part II


1
Object-Oriented ProgrammingPart II
  • COMP114
  • Thursday January 31

2
Announcements
  • 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

3
Topics
  • Last Time
  • Review of References
  • Object-Oriented Programming
  • Encapsulation
  • This Class
  • Little more on Overloading
  • Inheritance
  • Protection

4
Overloading
  • Cant overload by return type in Java
  • Example
  • Compiler will complain.

int getLength( ) ... double getLength( ) ...
5
Inheritance
  • 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

6
Inheritance
  • Derive new class from an existing class.

Is-A Relationship
7
Nomenclature
  • Superclass
  • Subclass
  • Parent class
  • Child class
  • Base class
  • Derived class

8
Superclass Person
  • public class Person
  • public Person(String theName)
  • name theName
  • private String name null

9
Subclass 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

10
Why 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?

11
Derived Class
  • Can add methods
  • Can add new instance variables
  • Can also override methods
  • Cannot remove methods or instance variables

12
Employee New Constructor
  • public class Employee extends Person
  • Employee(String theName, double sal)
  • super(theName)
  • salary sal
  • private double salary 0

Constructor of superclass
13
Rules 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

14
Remember Superclass
  • public class Person
  • public Person(String theName)
  • name theName

15
Subclass 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
16
Access
  • public class Person
  • public Person(String theName)
  • name theName
  • private String name null

Subclasses cant modify directly
17
Why 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.

18
Private 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.

19
Dynamic Binding
  • A reference variable of the superclass can also
    reference subclasses

Person p p new Employee(Lastra, 1234)
20
Example 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 )

21
Polymorphism
  • From Websters
  • the quality or state of being able to assume
    different forms
  • Reference variable that is polymorphic can
    reference objects of several types

22
Parameters
  • 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)
23
Multiple Types
  • An object is in some sense
  • of its own type and
  • also of its ancestors types.
  • Is there an Adam/Eve?

24
Object 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.

25
Inheritance Hierarchies
  • Inheritance proceeds along generations
  • So
  • Faculty, which is derived from Employee, which is
    derived from Person
  • Can use the Person methods

26
Has-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

27
Summary
  • Derived class extends base class
  • Can add data and methods
  • Can override superclass methods
  • If not overridden, methods are inherited

28
Next Time
  • More on Objects
  • Abstract methods
  • Review for test
Write a Comment
User Comments (0)
About PowerShow.com