0% found this document useful (0 votes)
23 views33 pages

Chapter 05

Uploaded by

sally D.allah
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)
23 views33 pages

Chapter 05

Uploaded by

sally D.allah
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/ 33

Chapter 5

Object-Oriented Programming
 Develop code that implements tight
encapsulation, loose coupling, and high
cohesion
 Develop code that demonstrates the use of
polymorphism
 Develop code that declares and/or invokes
overridden or overloaded methods and code
 Develop code that implements “is-a” and/or
“has-a” relationships
 Understanding Object-Oriented Relationships

 Implementing Polymorphism
 Conversion of Object Reference Types
 Using Method Overriding and Overloading
A class and its members are related to each
other: the class has data variables in it
 Classes themselves are related to each other: a
class is derived from another class
 Two kinds of relationships: is-a and has-a,
correspond to the two properties of classes:
inheritance and data abstraction
 The is-a relationship corresponds to
inheritance
 A class is in an is-a relationship with any class
up in its hierarchy tree
 corresponds to an object-oriented
characteristic called encapsulation: the data
and the methods are combined into a class
 Encapsulation facilitates data abstraction, the
relationship between a class and its data
members.
 Tight encapsulation: all data members of the
class should be declared private
 Data abstraction (data hiding): the data is
hidden from the user
 makes the code more reliable, robust, and
reusable
 refers
to minimizing the dependence of an
object on other objects
 canchange the implementation of a class
without affecting the other classes
 make the code extensible and easy to maintain
 demands that a class keep its members private
and that the other class access them through
getters and setters
 access the public member of another class
directly
 refers to how a class is structured
A cohesive class is a class that performs a set
of closely related tasks
 Ifa class is performing a set of unrelated tasks
(a non-cohesive class), you should consider
writing multiple classes - reshuffling the tasks
 Understanding Object-Oriented Relationships
 Implementing Polymorphism
 Conversion of Object Reference Types
 Using Method Overriding and Overloading
 Theis-a relationship between a superclass and
a subclass: you can substitute an object of the
subclass for an object of the superclass
A superclass can have multiple subclasses,
giving different meaning to the same thing
 Thecapability to convert an object reference
from one type to another type
 Understanding Object-Oriented Relationships
 Implementing Polymorphism
 Conversion of Object Reference Types
 Using Method Overriding and Overloading
 Implicit Conversion of Object Reference Types:
• Assignment Conversion
• Method Call Conversion
 Explicit
Conversion of Object Reference Types -
object reference casting
 The rules are the same as the rules for
assignment conversion.
 The passed-in argument types are converted
into the method parameter types when the
conversion is valid.
 Because object reference conversion may
involve reference types and object types:
• Some of the casting rules relate to the
reference type (known at compile time) and
therefore can be enforced at compile time.
• Some of the casting rules relate to the
object type (not known at compile time) and
therefore can only be enforced at runtime.
 Classes: one class must be a subclass of the
other.
 Arrays: the elements of both the arrays must
be object reference types. Also, the object
reference type of the source array must be
convertible to the object reference type of the
target array.
 The casting between an interface and an
object that is not final is always allowed
 Ifthe target type is a class, then the class of
the expression being converted must be either
the same as the target type or its subclass.
 Ifthe target type is an interface, then the class
of the expression being converted must
implement the target type.
 Understanding Object-Oriented Relationships
 Implementing Polymorphism
 Conversion of Object Reference Types
 Using Method Overriding and Overloading
 Overridingallows you to modify the behavior
of an inherited method to meet the specific
needs of a subclass: extensibility
 Overloading allows you to use the same
method name to implement different (but
related) functionalities: flexibility
 to change the behavior of an inherited method
 redefine the method by keeping the same
signature but rewriting the body
 The rules for overriding a method:

• You cannot override a method that has the final


modifier.
• You cannot override a static method to make it
non-static.
• The overriding method and the overridden method
must have the same return type
 The number of parameters and their types:
same
 You cannot override a method to make it less
accessible
 If the overriding method has a throws clause:
• The overridden method must have a throws clause
• Each exception included in the throws clause of the
overriding method must be either one of the
exceptions in the throws clause of the overridden
method or a subclass of it.
 The same task is to be performed in slightly
different ways under different conditions
 Multiple methods in a class with identical
names
 Notwo overloaded methods could have the
same parameter types in the same order
 Thereturn types in overloaded methods may
be the same or different

You might also like