Week 8 Lecture 3 Inheritance and Polymorphism 1



















![Animal example Java code class Test. Animal { public static void main(String args[]) { Animal example Java code class Test. Animal { public static void main(String args[]) {](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-20.jpg)
![Animal example Java code class Test. Animal { public static void main(String args[]) { Animal example Java code class Test. Animal { public static void main(String args[]) {](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-21.jpg)



















![to invoke Parent class method public class Test. Super{ public static void main(String args[]){ to invoke Parent class method public class Test. Super{ public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-41.jpg)
![to invoke Parent class method public class Test. Super{ public static void main(String args[]){ to invoke Parent class method public class Test. Super{ public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-42.jpg)

![to invoke Parent class method public class Test. Super{ public static void main(String args[]){ to invoke Parent class method public class Test. Super{ public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-44.jpg)










![to. String() method class Test. Animal { public static void main(String args[]) { Cat to. String() method class Test. Animal { public static void main(String args[]) { Cat](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-55.jpg)

- Slides: 56

Week 8 Lecture -3 Inheritance and Polymorphism 1

Lecture Outline • Inheritance – Definition and basic terminology – Various forms of Inheritance • Inheritance in Java – examples – use of the super keyword – access modifier (perhaps recap) – method overriding (vs. overloading) – Object class and to. String() method 2

Inheritance 3

Inheritance contd. • Inheritance: It a parent-child (is-a) relationship between classes • The concept allows sharing of the behavior of the parent class into its child classes – Code reuse • Child class can add new behavior or override existing behavior from parent (to be more specilaised) 4

Inheritance contd. • Inheritance creates is-a relation. – if A is-a B than A could extend B • Head is-a Lecturer. • Everything that a lecturer can do, the Head also can do. • Head has all the functionality of a Lecturer and some more. 5

Inheritance terminology • Super class/Base class/Parent class: terms to describe the parent in the relationship, which shares its functionality • Sub class/Derived class/Child class: terms to describe the child in the relationship, which accepts functionality from its parent • Extended/Inherited/Derived: become a subclass of another class 6

Various forms…of Inheritance Animal Cat Single Inheritance: one derived class inherits from one base class. 7

Various forms…of Inheritance contd. Animal Cat Persian. Cat Multilevel Inheritance: subclass acts as a base class for other classes ©Abdur Rakib 2017 8

Various forms…of Inheritance contd. Animal Cat Donkey Horse Persian. Cat Hierarchical Inheritance: multiple subclasses inherit from one base class. 9

Various forms…of Inheritance contd. Animal Cat Persian. Cat Donkey Horse Multiple Inheritance: Java does not support multiple inheritance. Other languages (C++) allow multiple inheritance

Inheritance in Java • In Java, inheritance is implemented using the keyword extends public class Cat extends Animal{ } – the objects of Cat class will now receive all of the state (fields) and behavior (methods) of the parent class Animal – constructors and static methods/fields are not inherited – by default, a class's parent is Object

Inheritance Example Animal +eat(): void Cat +meow(): void

Animal example Java code class Animal{ public void eat(){ System. out. println("eating. . . "); } } class Cat extends Animal{ public void meow(){ System. out. println("meowing. . . "); } } class Test. Animal{ public static void main(String args[]){ Cat cat=new Cat(); cat. meow(); cat. eat(); } } Output: meowing. . . eating. . . 13

Animal example Java code class Animal{ public void eat(){ System. out. println("eating. . . "); } } class Cat extends Animal{ String name; public Cat(String name){ this. name=name; } public void meow(){ System. out. println(name+" meowing. . . "); } } } ©Abdur Rakib 2017 class Test. Animal{ public static void main(String args[]){ Cat cat=new Cat("Bela"); cat. meow(); cat. eat(); } }

Animal example Java code class Animal{ public void eat(){ System. out. println("eating. . . "); } } class Cat extends Animal{ String name; public Cat(String name){ this. name=name; } public void meow(){ System. out. println(name+" meowing. . . "); } } } class Test. Animal{ public static void main(String args[]){ Cat cat=new Cat("Bela"); cat. meow(); cat. eat(); } } Output: Bela meowing. . . eating. . .

Hierarchical Inheritance Example Animal -weight: int -name: String +get. Weight(): void Bird Cat +meow(): void + fly() : void

Animal example Java code public class Animal { int weight; String name; / / constructor public Animal(){} public Animal(int weight, String name) { this. weight = weight; this. name = name; } / / methods public void get. Weight() { System. out. println(name+"’s weight: " + weight); } }

Animal example Java code contd. class Cat extends Animal { public Cat(int weight, String name) { super(weight, name); } public void meow() { System. out. println(name+ " meowing"); } }

Animal example Java code contd. class Bird extends Animal { public Bird(int weight, String name) { super(weight, name); } public void fly() { System. out. println(name+ " flying"); } }
![Animal example Java code class Test Animal public static void mainString args Animal example Java code class Test. Animal { public static void main(String args[]) {](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-20.jpg)
Animal example Java code class Test. Animal { public static void main(String args[]) { Animal animal = new Animal(); Cat cat = new Cat(500, "Bela"); Bird bird = new Bird(50, "Happy"); cat. get. Weight(); cat. meow(); bird. get. Weight(); bird. fly(); } } Output: Bela’s weight: 500 Bela meowing Happy’s weight: 50 Happy flying
![Animal example Java code class Test Animal public static void mainString args Animal example Java code class Test. Animal { public static void main(String args[]) {](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-21.jpg)
Animal example Java code class Test. Animal { public static void main(String args[]) { Animal animal = new Animal(); Cat cat = new Cat(500, "Bela"); Bird bird = new Bird(50, "Happy"); cat. get. Weight(); cat. meow(); bird. get. Weight(); bird. fly(); animal. get. Weight(); } } Output: Bela’s weight: 500 Bela meowing Happy’s weight: 50 Happy flying null’s weight: 0

How constructors work in inheritance • In Java, constructor of base class with no argument gets automatically called in derived class constructor. class Base. Class { Base. Class() { System. out. println("Base Class Constructor"); } } class Derived. Class extends Base. Class { Derived. Class() { System. out. println("Derived Class Constructor"); } }

How constructors work in inheritance • In Java, constructor of base class with no argument gets automatically called in derived class constructor. public class Test. Constructor { public static void main(String[] args) { Derived. Class dc = new Derived. Class(); } } Output: Base Class Constructor Derived Class Constructor

How about parameterised constructors • We can call them using super() [already shown in the previous Animal example] • In this case base class constructor call must be the first line in derived class constructor class Base. Class { double d; Base. Class(double d) { this. d = d; } } class Derived. Class extends Base. Class { int i; Derived. Class(double d, int i) { super(d); this. i=i; } public void display() { System. out. println("d = "+d+", i = "+i); } } 24

How about parameterised constructors • We can call them using super() • In this case base class constructor call must be the first line in derived class constructor public class Test. Super { public static void main(String[] args) { Derived. Class dc = new Derived. Class(10. 25, 20); dc. display(); } } Output: d = 10. 25, i = 20

Observation(1) class Base. Class { double d; Base. Class(double d) { this. d = d; } } class Derived. Class extends Base. Class { } public class Test. Super { public static void main(String[] args) { Derived. Class dc = new Derived. Class(); } } ? ?

Observation(1) contd. If we specify a constructor explicitly, as in Base. Class, the Java compiler will not create a default constructor for that class. If we don't specify a constructor explicitly, as in Derived. Class, the Java compiler will create a default constructor for that class like this: Derived. Class() { super(); }

Observation(2) class Base. Class { } class Derived. Class extends Base. Class { } public class Test. Super { public static void main(String[] args) { Derived. Class dc = new Derived. Class(); System. out. println("Things are OK"); } } Output: ? ?

Observation(2) contd. class Base. Class { } class Derived. Class extends Base. Class { } public class Test. Super { public static void main(String[] args) { Derived. Class dc = new Derived. Class(); System. out. println("Things are OK"); } } Output: Things are OK

Observation (3) • Why constructors cannot be inherited? Justify Exercise!

Image source: google 31

Access Modifiers Access Modifier Class or member can be referenced by… public methods of the same class, and methods of other classes private methods of the same class only protected methods of the same class, methods of subclasses, and methods of classes in the same package No access modifier (package-access/ package-private) methods in the same package only

public vs. private • Classes are usually declared to be public • Instance variables are usually declared to be private • Methods that will be called by the client of the class are usually declared to be public • Methods that will be called only by other methods of the class are usually declared to be private • APIs of methods are published (made known) so that clients will know how to instantiate objects and call the methods of the class

What You Can Do in a Subclass • A subclass inherits all of the public, protected, package-private members of its base • You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. • You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. • A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. • And many more….

More use of the super keyword • can be used to refer to base class's methods, variables, constructors to call them – needed when there is a name conflict (hiding/shadowing) with current class • useful when overriding and you want to keep the old behavior but add new behavior to it • syntax: super(args); //call parent’s constructor super. field. Name //access parent’s field super. method. Name(args); //access parent’s method

to invoke Parent’s class variable class Base. Class { public int x = 10; } class Sub. Class extends Base. Class { int x = 100; public void display(){ System. out. println(x); } } public class Test. Super{ public static void main(String args[]){ Sub. Class s= new Sub. Class(); s. display(); } } Output: ? ?

to invoke Parent class variable class Base. Class { public int x = 10; } class Sub. Class extends Base. Class { int x = 100; public void Display(){ System. out. println(x); } } public class Test. Super{ public static void main(String args[]){ Sub. Class s= new Sub. Class(); s. Display(); } } Output: 100

to invoke Parent’s class variable • In the program we have the same variable “x” declared in both base class and sub class • A class can declare a variable with the same name as an inherited variable from its parent class, thus "hiding" or shadowing the inherited version • referring to the variable by name will use the one closest in scope

to invoke Parent class variable class Base. Class { public int x = 10; } class Sub. Class extends Base. Class { int x = 100; public void display(){ System. out. println(super. x); } } public class Test. Super{ public static void main(String args[]){ Sub. Class s= new Sub. Class(); s. display(); } } Output: 10

to invoke Parent class method class Base. Class { public void display(){ System. out. println("Base class"); } } class Sub. Class extends Base. Class { public void display(){ System. out. println("Sub class"); } public void print(){ display(); } }
![to invoke Parent class method public class Test Super public static void mainString args to invoke Parent class method public class Test. Super{ public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-41.jpg)
to invoke Parent class method public class Test. Super{ public static void main(String args[]){ Sub. Class s= new Sub. Class(); s. print(); } } Output: ? ?
![to invoke Parent class method public class Test Super public static void mainString args to invoke Parent class method public class Test. Super{ public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-42.jpg)
to invoke Parent class method public class Test. Super{ public static void main(String args[]){ Sub. Class s= new Sub. Class(); s. print(); } } Output: Sub class • display() method is overridden. • The argument list and return type should be exactly the same in both base and sub classes.

to invoke Parent class method class Base. Class { public void display(){ System. out. println("Base class"); } } class Sub. Class extends Base. Class { public void display(){ System. out. println("Sub class"); } public void print(){ super. display(); } }
![to invoke Parent class method public class Test Super public static void mainString args to invoke Parent class method public class Test. Super{ public static void main(String args[]){](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-44.jpg)
to invoke Parent class method public class Test. Super{ public static void main(String args[]){ Sub. Class s= new Sub. Class(); s. print(); } } Output: Base class

Overriding • Overriding means modifying the implementation of the method with same signature. • Subclass can override a method implementation to provide a different version than parent. • Subclass can add some information to parent version of the method. • Subclass can completely re-define the parent’s method.

Overloading Vs. Overriding • Overloading: more than one method in the same class with the same name but different signatures • Overriding: same name and same signatures method in the base and sub classes • Overloading allows us defining a similar operation in different ways for different data • Overriding allows us defining a similar operation in different ways for different object types

Overloading Vs. Overriding – Overloading deals with multiple methods in the same class with the same name but different signatures. – Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature. – Overloading lets you define a similar operation in different ways for different data. – Overriding lets you define a similar operation in different ways for different object types.

The Object Class

The Object Class • Java defines the class java. lang. Object that is defined as a superclass for all classes • All classes directly or indirectly extend the Object class • Hence , all classes inherit Object’s methods.

The Object Class contd. • The following two class definitions are equivalent: class A extends Object { { //code } }

Methods of Object Class • The Object class defines a set of methods that are inherited by all classes, including equals(Object o), get. Class(), to. String() • to. String() method that is used whenever we want to get a String representation of an object • When we define a new class, we can override the to. String() method in order to have a suitable representation of the new type of objects as Strings

to. String() method Animal Cat • Consider the simple example

to. String() method public class Animal { int weight; String name; / / constructor public Animal(){} public Animal(int weight, String name) { this. weight = weight; this. name = name; } / / methods @Override public String to. String(){//overriding the to. String() method return name+"’s weight: " + weight; } }

to. String() method lass Cat extends Animal { public Cat(int weight, String name) { super(weight, name); } public void meow() { System. out. println(name+ " meowing"); } }
![to String method class Test Animal public static void mainString args Cat to. String() method class Test. Animal { public static void main(String args[]) { Cat](https://slidetodoc.com/presentation_image_h2/81bcedd77803c5ace2373ee86aee1a6f/image-55.jpg)
to. String() method class Test. Animal { public static void main(String args[]) { Cat cat = new Cat(500, "Bela"); System. out. println(cat); cat. meow(); } } Output: Bela’s weight: 500 Bela meowing

Reference • Chapter 11, Introduction to Java. Programming, Liang.