inheritance and polymorphism part 2 - abu khleif · inheritance and polymorphism –part 2 ......

18
Introduction to OOP with Java - AKF Sep 2017 AbuKhleiF - www.abukhleif.com 1 Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 www.abukhleif.com Inheritance and Polymorphism – Part 2 Instructor: AbuKhleif, Mohammad Noor Sep 2017 www.abukhleif.com Lecture 12:

Upload: buikien

Post on 20-Jul-2018

235 views

Category:

Documents


3 download

TRANSCRIPT

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 1

Introduction to OOP with Java

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

Inheritance and Polymorphism – Part 2Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

Lecture 12:

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 2

Instructor

• AbuKhleif, ‘Mohammad Noor’• Computer Engineer (JU 2012-2017)• Software Automation Engineer @ Atypon – John Wiley and

Sons Company - Jordan Branch

• Reach me at:• www.abukhleif.com• [email protected]• facebook.com/moh.noor94• twitter.com/moh_noor94

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

3

Course

• Java SE Basics• Object Oriented Programming• Course Page:

www.abukhleif.com/courses/java-101-sep-2017• Or, go to: www.abukhleif.com Courses Java 101 Course – Sep 2017

• Course Facebook Group:www.facebook.com/groups/AKF2017Java

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

4

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 3

Polymorphism

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

5

Polymorphism

• The three pillars of object-oriented programming are:• Encapsulation

• Inheritance

• Polymorphism

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

6

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 4

Polymorphism

• A class defines a type.

• A type defined by a subclass is called a subType.

• A type define by a superclass is called a superType.

• For example:• Circle is a subType of GeometricObject.

• GeometricObject is a superType of Circle.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

7

Polymorphism

• Inheritance enables a subclass to inherit features from its superclass with additional new features.

• A subclass is a specialization of its superclass.• Every instance of a subclass is also an instance of its superclass,

but not vice versa. • Therefore, an instance of a subclass can be used wherever its superclass

instance is used.

• For example, every circle is a geometric object, but not every geometric object is a circle.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

8

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 5

Polymorphism

• Polymorphism means that a variable of a superType can refer to a subType object.

• Example:public static void main(String [] args){displayObjectColor( new Circle () );displayObjectColor( new Rectangle() );}public static void displayObjectColor(GeometricObject object){ System.out.println(“Color is ” + object.getColor());}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

9

Dynamic Binding

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

10

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 6

Dynamic Binding

• A method can be implemented in several classes along the inheritance chain.

• The JVM decides which method is invoked at runtime.• This is known as dynamic binding.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

11

Dynamic Binding

• Dynamic binding works as follows:

• Suppose o is an instance of C1 , C2 , C3 ,...., Cn. As follows:

• If o invokes a method p:

• The JVM searches for the implementation of the method p in C1 , C2 , C3 ,...., Cn , in this order, until it is found.

• Once an implementation is found, the search stops and the first-found implementation is invoked.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

12

Cn Cn-1 . . . . . C2 C1

Object

Since o is an instance of C1, o is also an

instance of C2, C3, …, Cn-1, and Cn

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 7

Dynamic Binding

• Consider the following code:Object o = new GeometricObject(); System.out.println(o.toString());

• There ate two important terms in order to identify which toString() method will be invoked by o:• Declared type: the type that declares a variable.

• In the previous code example, o’s declared type is Object.

• Actual type: The actual class for the object referenced by the variable.• In the previous code example, o’s actual type is GeometricObject.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

13

Dynamic Binding

• Which method is invoked is determined by the object actual type.

• In other words, the JVM starts the search with the class that defines the actual type.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

14

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 8

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 15

public class PolymorphismDemo {public static void main(String[] args) {

m(new GraduateStudent());m(new Student());m(new Person());m(new Object());

}

public static void m(Object x) {System.out.println(x.toString());

}}

class GraduateStudent extends Student {}

class Student extends Person {public String toString() {

return "Student";}

}

class Person extends Object {public String toString() {

return "Person";}

}

Output:StudentStudentPersonjava.lang.object@9fa8988

Dynamic Binding

• Matching a method signature and binding a method implementation are two separate issues:• The declared type of the reference variable decides which method

to match at compile time.• The compiler finds a matching method according to the parameter type,

number of parameters, and order of parameters.

• The JVM dynamically binds the implementation of the method at runtime, decided by the actual type of the variable.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

16

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 9

Casting Objects

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

17

Casting Objects

• You have already used the casting operator to (convert?) variables of one primitive type to another.

• Casting can also be used to (convert?) an object of one class type to another within an inheritance hierarchy.

• In the preceding section, the statement m(new Student()); assigns the object new Student() to a parameter of the Object type. This statement is equivalent to:

Object o = new Student(); // Implicit casting

m(o);

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

18

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 10

Casting Objects

• The statement Object o = new Student(), known as implicit casting (up casting), is legal because an instance of Student is always automatically an instance of Object.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

19

Casting Objects

• When casting an instance of a superclass to a variable of its subclass, explicit casting (down casting) must be used:• To confirm your intention to the compiler; otherwise a compile

error will occur.

• Example:Object o = new Student();Student b = o; // causes compile error

Student b = (Student)o; //does not cause compile error

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

20

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 11

Casting Objects

• In order for casting to be successful, you must make sure that the object to be cast is an instance of the subclass.• If the superclass object is not an instance of the subclass, a

runtime ClassCastException occurs.

• It is a good practice, to ensure that the object is an instance of another object before attempting a casting.• This can be accomplished using the instanceof operator

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

21

The instanceof Operator

Object myObject = new Circle();

... // Some lines of code

/** Perform casting if myObject is an instance of Circle */

if (myObject instanceof Circle) {

System.out.println("The circle diameter is " +

((Circle)myObject).getDiameter());

// ...

}

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

22

The object member access operator (.) precedes the casting operator. Parentheses are used to ensure that casting is done before the (.) operator.

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 12

Why Casting is Necessary?

• In the previous example:• The variable myObject is declared Object.

• The declared type decides which method to match at compile time.• Using myObject.getArea() would cause a compile error, because the Object class does

not have the getArea method.

• Therefore it is necessary to cast myObject into the Circle type to tell the compiler that myObject is also an instance of Circle.

• Why not define, myObject as a Circle type in the first place?• To enable generic programming: it is a good practice to define a variable with a

supertype, which can accept a value of any subtype.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

23

Example

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com 24

public class Casting {

public static void main(String[] args) {

Object object1 = new Circle();

Object object2 = new Rectangle();

displayObject(object1);

displayObject(object2);

}

public static void displayObject(Object object) {

if (object instanceof Circle) {

System.out.println("The circle radius is " + ((Circle) object).getRadius());

}

if (object instanceof Rectangle) {

System.out.println("The rectangle width is " + ((Rectangle) object).getWidth());

System.out.println("The rectangle height is " + ((Rectangle) object).getHeight());

}

}

}

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 13

The protected Modifier and Visibility Modifiers, Revisit

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

25

The protected Modifier

• Remember that:• private members can be accessed only from inside the class.• public members can be accessed from any other classes.

• Often it is desirable:• To allow subclasses to access data fields or methods defined in the

superclass.• But not to allow non-subclasses to access these data fields and

methods.

• To accomplish this you can use the protected keyword (access modifier).

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

26

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 14

The protected Modifier

• A subclass may override a method defined in its superclass and increase its accessibility.• For example, change its accessibility from protected (as defined in

the superclass) to public.

• However, a subclass cannot weaken the accessibility of a method defined in the superclass.• For example, a method defined as public in the superclass should

be defined as public in the subclass.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

27

Visibility Modifiers, Revisit

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

28

private, none (if no modifier is used), protected, public

Visibility increases

Modifier

on members

in a class

Accessed

from the

same class

Accessed

from the

same package

Accessed

from a

subclass

Accessed

from a different

package

public

protected -

default - -

private - - -

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 15

Visibility Modifiers, Revisit

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

29

public class C1 {

public int x;

protected int y;

int z;

private int u;

protected void m() {

}

}

public class C2 {

C1 o = new C1();

can access o.x;

can access o.y;

can access o.z;

cannot access o.u;

can invoke o.m();

}

public class C3

extends C1 {

can access x;

can access y;

can access z;

cannot access u;

can invoke m();

}

package p1;

public class C4

extends C1 {

can access x;

can access y;

cannot access z;

cannot access u;

can invoke m();

}

package p2;

public class C5 {

C1 o = new C1();

can access o.x;

cannot access o.y;

cannot access o.z;

cannot access o.u;

cannot invoke o.m();

}

The final Modifier, Revisit

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

30

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 16

The final Modifier

• The final class cannot be extended:

final class Math {

...

}

• The final variable is a constant:

final static double PI = 3.14159;

• The final method cannot be overridden by its subclasses.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

31

Homework

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 17

Homework

Part 1- Implement the following classes using Java:

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

33

Homework

Part 2- In your Test Class:

• Define a method called findVehicle that takes as parameter an array of Vehicleobjects and one Vehicle object.• The method should return the array index that represents the element that equals the

passed object.• If no element in the array is found to equal the passed object, the method returns -1.

In your main method:

• Declare an array called vehicles that consists of 5 vehicles, in the first element create an object of type Vehicle, the second and third of type Car, and the fourth and fifth of type motorcycle.

• Invoke the printDetails method in a for loop for each element of the array.

• Use the method findVehicle to print the index of the object that equals a newly created Vehicle of type car in the array vehicles.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

34

Introduction to OOP with Java - AKF Sep 2017

AbuKhleiF - www.abukhleif.com 18

Homework Submission

• Submit only the .java files.

• Upload your file to the Facebook group.

• Submission due: Thursday, Oct 5 - 08:00 PM

• Late submission will not be reviewed by the instructor.

• Public solutions upload goal is to share knowledge, you can see other’s solutions, but, please, don’t cheat yourself!

• Don’t forget, your solution should be well-documented, well-designed, and well-styled.

Introduction to OOP with Java - AbuKhleiF www.abukhleif.com

35

- Eng. Asma Abdel Karim Computer Engineering Department, JU Slides and Sheets.- Liang, Introduction to Java Programming 10/e

Instructor: AbuKhleif, Mohammad Noor

Sep 2017

www.abukhleif.com

References:

End of Lecture =D