CS 102
Object Oriented Programming
Inheritance
Some slides are adapted from the originals available at
[Link]
Animal Class
2
¨ Assume that we have an animal class
¤ Attributes
n Name
n Color
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
3
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
4
¨ Assume that we have an animal class
¤ Attributes
n Name
n Color
¤ Behaviors
n Speak
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
5
¨ Assume that we have an animal class
¤ Attributes
n Name
n Color
¤ Behaviors
n Speak
n Do they all speak the same way?
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
6
¨ Assume that we have an animal class
¤ Attributes
n Name
n Color
¤ Behaviors
n Speak
n Do they all speak the same way?
n Dogs bark
n Cats meow
n Cows moo
n Ducks quack
n ....
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
7
¨ Assume that we have an animal class
¤ Attributes
n Name
n Color
¤ Behaviors
n Speak
n Do they all speak the same way?
n Dogs bark
n Cats meow How do we
n Cows moo implement them?
n Ducks quack
n ....
Ozyegin University - CS 102 - Object Oriented Programming
Different type of animals
8
¨ We will talk about two bad solutions:
¤ First way: Inside the same class
¤ Second way: Writing a different class
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Inside the same class
9
¨ Need to hold an additional attribute:
¤ Type of the animal
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Inside the same class
10
¨ Need to hold an additional attribute:
¤ Type of the animal
¨ Main:
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Inside the same class
11
¨ Speak function
¤ Check the type of the animal and speak accordingly
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Inside the same class
12
¨ Main:
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Problem 1
13
¨ Many dog types, all bark differently
¤ Loudness
¤ Pace
¤ ...
¨ Do we need to
define another dog
type variable?
Source: [Link]
First Way – Problem 1
14
¨ Many dog types, all bark differently
¤ Loudness
¤ Pace
¤ ...
Source: [Link]
First Way – Problem 1
15
¨ Many dog types, all bark differently
¤ Loudness
¤ Pace
¤ ...
Ugly Code L
Source: [Link]
First Way – Problem 2
16
¨ Jumping is behavior of some animals
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Problem 2
17
¨ Jumping is behavior of some animals
¨ cat, dog can jump but not the fish...
Ozyegin University - CS 102 - Object Oriented Programming
First Way – Problem 2
18
¨ Jumping is behavior of some animals
¨ cat, dog can jump but not the fish...
¨ In main, we should not call jump for fish, but right
now we can as follows:
Ozyegin University - CS 102 - Object Oriented Programming
Different type of animals
19
¨ We will talk about two bad solutions:
¤ First way: Inside the same class
¤ Second way: Writing a different class
Ozyegin University - CS 102 - Object Oriented Programming
Second way: Class for each type
20
¨ We will have individual classes for each animal.
Ozyegin University - CS 102 - Object Oriented Programming
Second way: Class for each type
21
Cat Dog
Second way: Class for each type
22
Second way: Class for each type
23
Code repetition L.
It is hard to keep the common code consistent
Different type of animals
24
¨ We will talk about two bad solutions:
¤ First way: Inside the same class
¤ Second way: Writing a different class
Ozyegin University - CS 102 - Object Oriented Programming
Different type of animals
25
¨ We will talk about two bad solutions:
¤ First way: Inside the same class
¤ Second way: Writing a different class
¨ A good solution is to use inheritance
¤ Keep the common attributes and functionalities in one
class
¤ Split only the different attributes and functionalities in
different classes.
Ozyegin University - CS 102 - Object Oriented Programming
Inheritance
26
¨ A class can inherit some of its attibutes and
behaviors from another class.
Ozyegin University - CS 102 - Object Oriented Programming
Inheritance
27
¨ A class can inherit some of its attibutes and
behaviors from another class.
¨ A derived class inherits from the base class.
¨ A sub class inherits from the super class.
Ozyegin University - CS 102 - Object Oriented Programming
Inheritance
28
¨ A class can inherit some of its attibutes and
behaviors from another class.
¨ A derived class inherits from the base class.
¨ A sub class extends the super class.
Ozyegin University - CS 102 - Object Oriented Programming
Inheritance
29
¨ Keep the common attributes and functionalities in
one class
¤ Animal class
n name and color
n setter and getter functions
¨ Split only the different attributes and functionalities
in different classes.
¤ Cat, dog, cow ... classes
n speak, jump function
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
30
Ozyegin University - CS 102 - Object Oriented Programming
Cat and Dog Classes
31
Ozyegin University - CS 102 - Object Oriented Programming
Cat and Dog Classes
32
inherits from
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
33
Classes in Java form hierarchies.
Animal
Cat Dog Cow Fish
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
34
Base/Super Class
Animal
Cat Dog Cow Fish
Derived/Sub Classes
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
35
Animal class represent all animal objects.
Animal
Cat Dog Cow Fish
The four subclasses correspond to particular animal
type object.
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
36
Animal
Cat Dog Cow Fish
This class diagram shows that Cat, Dog, Cow and Fish
is also an Animal but the inverse is NOT TRUE. Any
animal is not a Cat, any Animal is not a Dog, etc.
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
37
Can the Animal class be also a derived class?
Animal
Cat Dog Cow Fish
When you define a new class in Java, that class
automatically inherits the behavior of its superclass.
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
38
Can the Animal class be also a derived class?
Animal
Cat Dog Cow Fish
If no superclass is defined, by default, the class will
inherit from the Object class.
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
39
Object
Animal
Cat Dog Crow Horse
Ozyegin University - CS 102 - Object Oriented Programming
Extending from Object Class
40
public class Animal { ...
¨ is the same as
public class Animal extends Object { ...
Ozyegin University - CS 102 - Object Oriented Programming
Extending from Object Class
41
public class Animal { ...
¨ is the same as
public class Animal extends Object { ...
¨ The extends clause on the header line specifies the
name of the superclass. If the extends clause is
missing, the new class becomes a direct subclass of
Object, which is the root of Java’s class hierarchy.
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
42
Object
Animal
Cat Dog Crow Horse
¨ Except for the class named Object that stands at
the top of the hierarchy, every class in Java is a
subclass of some other class.
Ozyegin University - CS 102 - Object Oriented Programming
Class Hierarchy
43
Object
Animal
Cat Dog Crow Horse
¨ A class can have many subclasses, but each class
has only one superclass.
Ozyegin University - CS 102 - Object Oriented Programming
Lets get back to our Animal class
44
Ozyegin University - CS 102 - Object Oriented Programming
Derived Classes
45
¨ Instead of calling the set methods can we just
modify the name and color directly?
Ozyegin University - CS 102 - Object Oriented Programming
Derived Classes
46
¨ What is the problem?
Ozyegin University - CS 102 - Object Oriented Programming
Derived Classes
47
¨ The name and color is private therefore cannot be
accessed from the derived/sub class Cat.
¨ Therefore we need to use the getters and setters
method to reach these private class instances.
Ozyegin University - CS 102 - Object Oriented Programming
What is inherited?
48
¨ All class instances and functions of the base/super
class are inherited.
¨ But not all of them are visible from the sub class
Ozyegin University - CS 102 - Object Oriented Programming
What is inherited?
49
¨ All class instances and functions of the base/super
class are inherited.
¨ But not all of them are visible from the sub class
¤ Public and protected ones are visible
¤ Default and private ones are NOT visible
n These can be accessed only through getter and setter
functions.
Ozyegin University - CS 102 - Object Oriented Programming
Remember Visibility
50
Alpha Beta AlphaSub Gamma
public
protected
default
private
Source: [Link]
Remember Visibility
51
Alpha Beta AlphaSub Gamma
public Y Y Y
protected Y Y N
default Y N N
private N N N
Source: [Link]
Lets get back to our Animal class
52
Ozyegin University - CS 102 - Object Oriented Programming
Constructors
53
¨ What happens inside this constructor?
¨ Initially the default constructor of the super class is
called by compiler.
¨ Whenever you create an object of an extended class,
Java must call some constructor for the superclass object
to ensure that its structure is correctly initialized.
Ozyegin University - CS 102 - Object Oriented Programming
Constructors
54
¨ Below two are the same!
Ozyegin University - CS 102 - Object Oriented Programming
55
¨ Similar to this();
¨ this(arg); // same class constructor call
¨ super(arg); // super class constructor call
Ozyegin University - CS 102 - Object Oriented Programming
56
¨ Similar to this();
¨ this(arg); // same class constructor call
¨ super(arg); // super class constructor call
¨ Both of these need to be used in the first line of the
constructor.
¨ If not, then default super() will be included by
compiler
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Call
57
Object
Animal
Cat Dog Crow Horse
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Call
58
Object
Animal
Default
constructor
Cat Dog Crow Horse
calls super();
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Call
59
Object
If the superclass does not define any explicit
constructors, Java automatically provides a
Animal
default constructor with an empty body.
Default
constructor
Cat Dog Crow Horse
calls super();
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Call
60
Object
Object
Animal
Cat Dog Crow Horse
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Call
61
Object
Animal
Animal
Object
Cat Dog Crow Horse
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Call
62
Object Cat
Animal
Animal
Object
Cat Dog Crow Horse
Ozyegin University - CS 102 - Object Oriented Programming
Explicit Animal Constructor
63
¨ Lets have an explicit Animal constructor
Ozyegin University - CS 102 - Object Oriented Programming
Explicit Animal Constructor
64
¨ Lets have an explicit Animal constructor
Ozyegin University - CS 102 - Object Oriented Programming
Explicit Animal Constructor
65
¨ Lets have an explicit Animal constructor
Ozyegin University - CS 102 - Object Oriented Programming
66
Ozyegin University - CS 102 - Object Oriented Programming
Constructor Calls
67
¨ Java therefore invokes the superclass constructor in
one of the following ways:
¤ Classes that begin with an explicit call to this invoke
one of the other constructors for this class, delegating
responsibility to that constructor for making sure that
the superclass constructor gets called.
¤ Classes that begin with a call to super invoke the
constructor in the superclass that matches the argument
list provided.
¤ Classes that begin with no call to either super or this
invoke the default superclass constructor with no
arguments.
Ozyegin University - CS 102 - Object Oriented Programming
Example
68
¨ What is the
output?
Ozyegin University - CS 102 - Object Oriented Programming
Example
69
¨ What is the
output?
Ozyegin University - CS 102 - Object Oriented Programming
Lets implement some functions
70
¨ Animals speak differently, so speak function needs
to be implemented differently.
Ozyegin University - CS 102 - Object Oriented Programming
Animal Class
71
¨ In animal class we don’t have a speak() function
Ozyegin University - CS 102 - Object Oriented Programming
Cat Class
72
¨ Speak function implemented within the Cat class.
Ozyegin University - CS 102 - Object Oriented Programming
Cat Class
73
¨ Speak function implemented within the Cat class.
OUTPUT:
Ozyegin University - CS 102 - Object Oriented Programming
toString method
74
¨ Can we call the toString method from a cat object?
Ozyegin University - CS 102 - Object Oriented Programming
toString method
75
¨ Can we call the toString method from a cat object?
¨ Yes, I can. What will be the output?
Ozyegin University - CS 102 - Object Oriented Programming
toString method
76
¨ Can we call the toString method from a cat object?
¨ Yes, I can. What will be the output?
Ozyegin University - CS 102 - Object Oriented Programming
toString method
77
¨ Can the Cat class has its own toString function?
Ozyegin University - CS 102 - Object Oriented Programming
toString method
78
¨ Can the Cat class has its own toString function?
Ozyegin University - CS 102 - Object Oriented Programming
Overriding (Overwriting)
79
¨ Yes, it can. It is called function overwriting.
¨ A subclass may redefine a method that is defined
by a superclass. In this case, it is said that the
subclass overrides the method.
Ozyegin University - CS 102 - Object Oriented Programming
Overriding (Overwriting)
80
¨ When one class extends another, the subclass is
allowed to override method definitions in its
superclass. Whenever you invoke that method on
an instance of the extended class, Java chooses the
new version of the method provided by that class
and not the original version provided by the
superclass.
Ozyegin University - CS 102 - Object Oriented Programming
Overriding (Overwriting)
81
¨ The decision about which version of a method to use
is always made on the basis of what the object in
fact is and not on what it happens to be declared
as at that point in the code.
¨ Will be covered more in the upcoming weeks!
Ozyegin University - CS 102 - Object Oriented Programming
What will be the output?
82
Ozyegin University - CS 102 - Object Oriented Programming
What will be the output?
83
Ozyegin University - CS 102 - Object Oriented Programming
toString method
84
¨ Cat is still an animal. How can I print animal
representation as well as the cat representation.
¨ How can I print out the following from the above
main?
Ozyegin University - CS 102 - Object Oriented Programming
toString method
85
Ozyegin University - CS 102 - Object Oriented Programming
86
¨ If you need to invoke the original version of a
method, you can do so by using the keyword super
as a receiver.
¨ Ex: [Link]()
Ozyegin University - CS 102 - Object Oriented Programming
Overloading vs. Overriding
88
¨ What is the difference between these two?
Ozyegin University - CS 102 - Object Oriented Programming
Overloading vs. Overriding
89
¨ What is the difference between these two?
¨ Overloading
¤ Same class has the same function name but with
different parameters.
¨ Overwriting
¤ Subclass has the same function signature (name and
parameters) with the superclass
Ozyegin University - CS 102 - Object Oriented Programming
toString method in Animal Class
90
¨ Is toString an overriding function or not?
Ozyegin University - CS 102 - Object Oriented Programming
toString method in Animal Class
91
¨ Is toString an overriding function or not?
¤ Yes it overrides the toString method of the Object class
Ozyegin University - CS 102 - Object Oriented Programming
Ozet
93
¨ Inheritence(kalitim): Bir sınıfta (class) tanımlanmış değişkenlerin ve/veya metotların
(fonksiyon, procedure) yeniden tanımlanmasına gerek olmaksızın yeni bir sınıfa
taşınabilmesidir. Bunun için yapılan iş, bir sınıftan bir alt-sınıf (subclass) türetmektir.
Türetilen alt-sınıf, üst-sınıfta tanımlı olan bütün değişkenlere ve metotlara sahip olur.
Bu özeliğe kalıtım özeliği (inheritance) diyoruz.
¨ Ust siniflarin tum ozellikleri kaltimsal olarak alinabilir ama alt siniflar tarafindan bu
ozelliklerin tamami ulasilabilir durumda olmayabilir. (protected ve public tanimli
olanlar ulasilabilirken private ve default tanimlilar ulasilamaz.)
¨ super methodunun kullanımı iki türlü olur. Birincisi, üst-sınıfa ait nesne yaratmak
içindir. İkincisi, üst-sınıfın öğelerine erişmek içindir.
¨ Alt siniflarda constructorlarda this ve super kullanimina dikkat edilerek objeler
olusturulmalidir.
¨ Overwriting (cignemek): Ust sinifin içerdiği bir metodun alt sinif tarafından yeniden
tanımlanması durumudur. Overloading (Metotlarin asiri yuklenesi) ise ayni sinifta
ayni fonksiyon isminin farkli sayida arguman ile tanimlanmasidir.
Ozyegin University - CS 102 - Object Oriented Programming
94 Any Questions ?
Ozyegin University - CS 102 - Object Oriented Programming