Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 8 Lecture -3 Inheritance and Polymorphism

Similar presentations


Presentation on theme: "Week 8 Lecture -3 Inheritance and Polymorphism"— Presentation transcript:

1 Week 8 Lecture -3 Inheritance and Polymorphism
1

2 Lecture Outline Inheritance Inheritance in Java
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 toString() method 2

3 Inheritance 3

4 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

5 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

6 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

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

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

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

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

11 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

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

13 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 TestAnimal{ public static void main(String args[]){ Cat cat=new Cat(); cat.meow(); cat.eat(); Output: meowing... eating... } 13

14 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 TestAnimal{ public static void main(String args[]){ Cat cat=new Cat("Bela"); cat.meow(); cat.eat(); } ©Abdur Rakib 2017

15 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 TestAnimal{ public static void main(String args[]){ Cat cat=new Cat("Bela"); cat.meow(); cat.eat(); } Output: Bela meowing... eating...

16 Hierarchical Inheritance Example
Animal -weight: int -name: String +getWeight(): void Cat +meow(): void Bird + fly() : void

17 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 getWeight() System.out.println(name+"’s weight: " + weight);

18 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");

19 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");

20 Animal example Java code
class TestAnimal { public static void main(String args[]) { Animal animal = new Animal(); Cat cat = new Cat(500, "Bela"); Bird bird = new Bird(50, "Happy"); cat.getWeight(); cat.meow(); bird.getWeight(); bird.fly(); } Output: Bela’s weight: 500 Bela meowing Happy’s weight: 50 Happy flying

21 Animal example Java code
class TestAnimal { public static void main(String args[]) { Animal animal = new Animal(); Cat cat = new Cat(500, "Bela"); Bird bird = new Bird(50, "Happy"); cat.getWeight(); cat.meow(); bird.getWeight(); bird.fly(); animal.getWeight(); } Output: Bela’s weight: 500 Bela meowing Happy’s weight: 50 Happy flying null’s weight: 0

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

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

24 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 DerivedClass extends BaseClass { int i; DerivedClass(double d, int i) { super(d); this.i=i; } public void display() { System.out.println("d = "+d+", i = "+i); 24 class BaseClass { double d; BaseClass(double d) { this.d = d; } }

25 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 TestSuper { public static void main(String[] args) { DerivedClass dc = new DerivedClass(10.25, 20); dc.display(); } Output: d = 10.25, i = 20

26 Observation(1) class BaseClass { double d; BaseClass(double d) { this.d = d; } ?? class DerivedClass extends BaseClass { } public class TestSuper { public static void main(String[] args) { DerivedClass dc = new DerivedClass(); }

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

28 Observation(2) Output: ?? class BaseClass { }
class DerivedClass extends BaseClass { } Output: ?? public class TestSuper { public static void main(String[] args) { DerivedClass dc = new DerivedClass(); System.out.println("Things are OK"); }

29 Observation(2) contd. Output: Things are OK class BaseClass { }
class DerivedClass extends BaseClass { } Output: Things are OK public class TestSuper { public static void main(String[] args) { DerivedClass dc = new DerivedClass(); System.out.println("Things are OK"); }

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

31 Image source: google 31

32 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

33 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

34 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….

35 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.fieldName //access parent’s field super.methodName(args);//access parent’s method

36 to invoke Parent’s class variable
class BaseClass { public int x = 10; } class SubClass extends BaseClass int x = 100; public void display(){ System.out.println(x); public class TestSuper{ public static void main(String args[]){ SubClass s= new SubClass(); s.display(); } Output: ??

37 to invoke Parent class variable
class BaseClass { public int x = 10; } class SubClass extends BaseClass int x = 100; public void Display(){ System.out.println(x); public class TestSuper{ public static void main(String args[]){ SubClass s= new SubClass(); s.Display(); } Output: 100

38 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

39 to invoke Parent class variable
class BaseClass { public int x = 10; } class SubClass extends BaseClass int x = 100; public void display(){ System.out.println(super.x); public class TestSuper{ public static void main(String args[]){ SubClass s= new SubClass(); s.display(); } Output: 10

40 to invoke Parent class method
class SubClass extends BaseClass { public void display(){ System.out.println("Sub class"); } public void print(){ display(); class BaseClass { public void display(){ System.out.println("Base class"); }

41 to invoke Parent class method
public class TestSuper{ public static void main(String args[]){ SubClass s= new SubClass(); s.print(); } Output: ?? }

42 to invoke Parent class method
public class TestSuper{ public static void main(String args[]){ SubClass s= new SubClass(); 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.

43 to invoke Parent class method
class SubClass extends BaseClass { public void display(){ System.out.println("Sub class"); } public void print(){ super.display(); class BaseClass { public void display(){ System.out.println("Base class"); }

44 to invoke Parent class method
public class TestSuper{ public static void main(String args[]){ SubClass s= new SubClass(); s.print(); } Output: Base class }

45 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.

46 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

47 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.

48 The Object Class

49 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.

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

51 Methods of Object Class
The Object class defines a set of methods that are inherited by all classes, including equals(Object o), getClass(), toString() toString() 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 toString() method in order to have a suitable representation of the new type of objects as Strings

52 toString() method Animal Cat Consider the simple example

53 toString() 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 toString(){//overriding the toString() method return name+"’s weight: " + weight;

54 toString() method lass Cat extends Animal {
public Cat(int weight, String name) super(weight, name); } public void meow() System.out.println(name+ " meowing");

55 toString() method class TestAnimal {
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

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


Download ppt "Week 8 Lecture -3 Inheritance and Polymorphism"

Similar presentations


Ads by Google