[go: up one dir, main page]

0% found this document useful (0 votes)
17 views41 pages

Lec 9-12

object oriented programming class lecture

Uploaded by

a.mukherjee
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)
17 views41 pages

Lec 9-12

object oriented programming class lecture

Uploaded by

a.mukherjee
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/ 41

CS F213 Object Oriented Programming

Inheritance, Aggregation and Composition, and


Polymorphism

Aritra Mukherjee, Dept. of CSIS


The four pillars of OOP
Inheritance
● Inheritance is a mechanism in which one object acquires all the
properties and behaviors of a parent object. It is an important part of
OOPs (Object Oriented programming system).
● Inheritance represents the IS-A relationship which is also known as a
parent-child relationship.
● Reasons to use?
○ For Method Overriding (so runtime polymorphism can be achieved).
○ For Code Reusability.
Terms used in Inheritance
● Class: Recall previous classes!
● Sub Class/Child Class: Subclass is a class which inherits the other class.
It is also called a derived class, extended class, or child class.
● Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
● Reusability: is a mechanism which facilitates us to reuse the fields and
methods. We can use the same fields and methods already defined in the
previous class.
The keywords for inheritance in Java
childClass extends
● Extends: The extends keyword indicates parentClass{
that you are making a new class that //properties of
derives from an existing class. The parentclass + more

meaning of "extends" is to increase the }


functionality.
● Implements: The implements keyword childClass implements
indicates that you are implementing the parentClass{

contract asked by the existing class. It //ideas of parent


class in working state
“implements” the ideas.
}
Types of inheritance in Java [concrete and abstract]
But abstract and concrete classes cannot have…
For that we need interface
Single inheritance in java [what's the point?!]
Multilevel inheritance in java [still, what's the point?!]
Hierarchical inheritance in java [Oh yeah!]
Why multiple inheritance is not supported in java?
● To reduce the complexity and simplify the language
● Consider a scenario where A, B, and C are three classes. The C class
inherits A and B classes. If A and B classes have the same method and
you call it from child class object, there will be ambiguity to call the
method of A or B class.
● How python deals with it? In the case of multiple inheritance, a given
attribute is first searched in the current class if it’s not found then it’s
searched in the parent classes. The parent classes are searched in a
left-right fashion and each class is searched once.
What about abstract classes?
● The same rules applies, just
some functions are overridden
and are some implemented
● If all needs implementation, then
it goes to interface
Interface in Java
● The interface in Java is a mechanism to achieve abstraction. There can
be only abstract methods in the Java interface, not method body. It is
used to achieve abstraction and multiple inheritance in Java.
● Java Interface also represents the IS-A relationship.
● It cannot be instantiated just like the abstract class.
○ Since Java 8, we can have default and static methods in an
interface.
○ Since Java 9, we can have private methods in an interface.
Interface in Java
● The Java compiler adds public and abstract keywords before the
interface method. Moreover, it adds public, static and final keywords
before data members.
Interface in Java
Interface in Java
Interface in Java [Note: no ambiguity!]
Interface inheritance in Java [dreamer grandpa and papa!]
Default method in interface
Static method in interface [note the object declaration]

This kind of declaration is also


possible in interface
Tagger and Nested Interface

● Tagger: An interface which has no member is known as a marker or


tagged interface, for example, Serializable, Cloneable, Remote, etc. They
are used to provide some essential information to the JVM so that JVM
may perform some useful operation.
● Nested: An interface can have another interface which is known as a
nested interface. The fundamentals are same as that of inner class
Aggregation
● If a class have an entity reference, it is known as Aggregation.
● Aggregation represents HAS-A relationship.
● When use Aggregation?
○ Code reuse is also best achieved by aggregation when there is no
is-a relationship.
○ Inheritance should be used only if the relationship is-a is maintained
throughout the lifetime of the objects involved; otherwise,
aggregation is the best choice.
Aggregation
Aggregation
Composition
● In Java, composition is a design principle that represents a “whole-part”
relationship between classes.
● Unlike aggregation, which is a weaker form of association, composition
implies a stronger relationship where one class (the whole) is composed
of other classes or objects (the parts), and the parts cannot exist
independently outside the whole. In other words, when the whole is
destroyed, its parts are also destroyed.
Composition
Composition
Difference between Aggregation and Composition

Property Aggregation Composition

Strength ● Aggregation represents a weaker ● Composition represents a


relationship between classes. stronger relationship between
● It implies a “has-a” or “uses-a” classes.
relationship, where one class contains ● It implies a “whole-part”
a reference to another class, but the relationship, where one class
objects involved can exist (the whole) is composed of
independently. other classes (the parts), and
the parts cannot exist
independently outside of the
whole.
Difference between Aggregation and Composition

Property Aggregation Composition

Independence ● In aggregation, the “part” class (the ● In composition, the “part” objects
one contained within the other) can are created and managed by the
exist outside of the “whole” class (the “whole” class.
one containing the part). ● The parts cannot exist without
● The part can have a longer lifespan the whole, and their lifecycles
than the whole, and it may be used are tightly bound to the whole
by multiple instances of the whole object.
class.
Difference between Aggregation and Composition
Property Aggregation Composition

Ownership ● There is no ownership or ● Composition implies strong ownership


strong encapsulation in and encapsulation. The part objects are
aggregation. The part does owned and managed exclusively by the
not belong exclusively to the whole class.
whole. ● When the whole object is destroyed, the
● If the whole object is part objects are also destroyed.
destroyed, the part objects ● Example: An example of composition
can still exist. could be a house containing rooms.
● Example: An example of Rooms are created and managed by the
aggregation could be a house, and they cannot exist
university containing independently. When the house is
departments. Departments destroyed, the rooms cease to exist.
can exist independently and
can be part of multiple
universities.
Moral of the story
Polymorphism
● Polymorphism in Java is a concept by which we can perform a single
action in different ways. Polymorphism is derived from 2 Greek words:
poly and morphs. The word "poly" means many and "morphs" means
forms. So polymorphism means many forms.
● There are two types of polymorphism in Java: compile-time
polymorphism and runtime polymorphism. We can perform
polymorphism in java by method overloading and method overriding.
● We will only focus on runtime polymorphism here.
Runtime Polymorphism Concept
Runtime Polymorphism
● Runtime polymorphism or Dynamic Method Dispatch is a process in
which a call to an overridden method is resolved at runtime rather than
compile-time.
● If the reference variable of Parent class refers to the object of Child class,
it is known as upcasting. This is used here, example is discussed
previously.
Runtime Polymorphism
Runtime Polymorphism
Method overloading: changing no. of arguments
Method overloading: changing dataType of arguments
Method overloading by return type
Overloading is not possible with changing
return datatype only as it causes ambiguity
which function to call [though theoretically
Java could have done that by checking the
assignment datatype, provided the return
type is assigned to a variable which may
not be the case everytime!]
Method overloading: promotion of datatype
Java automatically promotes or
upcasts datatype when there is
no ambiguity and yet an operation
has a mix of datatypes

You might also like