0% found this document useful (0 votes)
2 views

lecture 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

lecture 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Principles of Object Oriented

Programming (CS2012)
Lecture 5
Fundamental Principles of OOP

1
Fundamental Principles of OOP
• Inheritance
– Inherit members from parent class
• Abstraction
– Define and execute abstract actions
• Polymorphism
– Access a class through its parent interface
• Encapsulation
– Hide the internals of a class

2
Recap
• What is inheritance?
– When a class is based on another class
– Is-a relationship
• How to achieve inheritance?
• Why inheritance?
• When should inheritance be used?
– Forward engineering Ahasclass has a set of students. Each student
an id. There are two types of
– Backward engineering students. Good students and bad
students. Good students are never late.
For bad students, a record is
3
maintained….
Recap
• What is abstraction?
– we know what an object does and how to use it,
but not how it does what it does
• How to achieve abstraction?
• Why abstraction?
• When should abstraction be used?

4
Polymorphism
• Computer Science - the provision of a
single interface to entities of different types
• Biology - when two or more clearly
different phenotypes exist in the same
population of a species (e.g. felidae or the cat
family)

5
Polymorphism
• The ability of an object to take on many forms
• Most common use -> a parent class reference
is used to refer to a child class object
• Subclasses of a class can define their own
unique behaviors and yet share some of the
same functionality of the parent class

6
7
Polymorphism in Java
• Any Java object that can pass more than one
IS-A test is considered to be polymorphic
• All Java objects are polymorphic??

8
Why Polymorphism
• Why handle an object of given type as object
of its base type?
– To invoke abstract operations
– To mix different related types in the same
collection
• E.g. List<Object> can hold anything
– To pass more specific object to a method that
expects a parameter of a more generic type
– To declare a more generic field which will be
initialized and "specialized" later

9
Example

• A deer IS-A(n) Animal


• A deer IS-A Vegetarian
• A deer IS-A Deer
• A deer IS-A(n) Object

10
Example

• How about Deer d = new Animal();???


• How about Animal a= new Deer();???

11
Revisiting Queue Example from Last
Week

12
13
Polymorphism and Method Overriding
Abstract Figure Abstract
class
method
+CalcSurface() : double

Concrete
class Square Circle
-x : int -x : int
-y : int -y : int
Overriden Overriden
-size : int -radius: int
method method

CalcSurface() CalcSurface()
{ {
return size * size; return PI * radius * raduis;
} }
14
14
Polymorphism and Method Overriding

15
16
17
18
Dynamic Binding
• Selecting the definition of a method to invoke at
runtime (i.e. which definition to bind to the
method call)
• Must match method name, number, order and
types of arguments
• Parameters may be of any type that is considered
a subtype (e.g. subclass) of the parameter type.
– e.g. public void m(Object x)
– e.g. public void p(double x) may accept any of the
numeric types for x (byte, short, int, long, float,
double) and implicitly perform a widening type
conversion (cast) if necessary
19
Casting
• Upcasting
– Converting the type of an object to a superclass (“up”
the inheritance/type hierarchy). Usually implicit, as
properties of superclasses are inherited by subclasses
automatically.
• e.g. Object o = new NonAcademic();
– Similarly, parameters of type Object may accept
objects of any other type, with an implicit cast to class
Object.
• Downcasting
– Converting the type of an object to a subclass (“down”
the inheritance hierarchy). Requires explicit casting
• e.g. NonAcademic a= (NonAcademic) o; 20
Casting
• NonAcademic a= (NonAcademic) o;

• Check if the cast would be successful


• What will happen if you do a wrong cast?

21

You might also like