chapter 05 polymorphism

17
Chapter 5 : Polymorphism

Upload: nurhanna-aziz

Post on 17-May-2015

486 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Chapter 05 polymorphism

Chapter 5 : Polymorphism

Page 2: Chapter 05 polymorphism

Objectives

•Student will learn about:▫overriding▫overloading

Page 3: Chapter 05 polymorphism

Polymorphism

~Ability to appear in many forms~The ability of reference variable to change behavior according to what object instance it is holding

Page 4: Chapter 05 polymorphism

Polymorphism Principle

“one interface, multiple methods”

Page 5: Chapter 05 polymorphism

Example•For example, given a base class shape,

polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results

•Subclassess of class can define their own unique behaviors but still share some of the same functionality of the parent class

Page 6: Chapter 05 polymorphism

Types of polymorphism• Overriding▫Methods of a subclass overwrite methods in

superclass▫ E.g the method in subclass using the same

name, same return type▫Run-time polymorphism

• Overloading▫Methods of subclass having same name but

different signatures▫ E.g the method in subclass using the same

name but different parameter list, return type▫Compile-time polymorphism

Page 7: Chapter 05 polymorphism

Example

CAR

VEHICLE

BUS

Page 8: Chapter 05 polymorphism

~ continuedpublic class Vehicle{ public int gear; public int speed;

public Vehicle(int startSpeed, int startGear) { gear = startGear; speed = startSpeed; }

public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -=

decrement; }public void speedUp(int increment) { speed += increment; }public void printDescription(){ System.out.println("\

nVehichle is in gear " + this.gear + " and travelling at a speed of " + this.speed + ". "); }

}

Page 9: Chapter 05 polymorphism

public class Car extends Vehicle{ Private String suspension;

public Car (int startSpeed, int startGear,int typeSuspension) { super(startSpeed,startGear);setSuspension(typeSuspension);}

Public String getSuspension(){return suspension;}Public void setSuspension(String typeSuspension)

{suspension=typeSuspension}

public void printDescription(){ Super.printDescription();System.out.println("\nThe car has " + this.getSuspension() + “

suspension. "); } }

Page 10: Chapter 05 polymorphism

public class Lorry extends Vehicle{ Private String width;

public Lorry (int startSpeed, int startGear,int tyrewidth) { super(startSpeed,startGear);setTyreWidth(tyrewidth);}

Public String getTyreWidth(){return width;}Public void setTyreWidth(String tyrewidth){width= tyrewidth}public void printDescription(){ Super.printDescription();System.out.println("\nThe Lorry has " + this. getTyreWidth() + “ inch

tyres. "); } }

Page 11: Chapter 05 polymorphism

Executes the codepublic class TestVehicle { public static void main(String[] args) {

Vehicle vehicle1, vehicle2,vehicle3; vehicle1 = new vehicle1(20, 10);

vehicle2 = new Car(20, 10, "Dual"); vehicle3 = new Lorry(40, 20, 23); vehicle1.printDescription(); vehicle2.printDescription(); vehicle3.printDescription(); } }

Page 12: Chapter 05 polymorphism

The outputsVehicle is in gear 20 and travelling at a

speed of 10.

Vehicle is in gear 20 and travelling at a speed of 10.

The car has Dual suspension.

Vehicle is in gear 40 and travelling at a speed of 40.

The Lorry has 23 inch tyres.

Page 13: Chapter 05 polymorphism

Rules for method overriding• The argument list should be exactly the same as that of the

overridden method.• The return type should be the same or a subtype of the

return type declared in the original overridden method in the super class.

• The access level cannot be more restrictive than the overridden method's access level. For example: if the super class method is declared public then the overridding method in the sub class cannot be either private or public. However the access level can be less restrictive than the overridden method's access level.

• Instance methods can be overridden only if they are inherited by the subclass.

• A method declared final cannot be overridden.• A method declared static cannot be overridden but can be re-

declared.• If a method cannot be inherited then it cannot be overridden.

Page 14: Chapter 05 polymorphism

~continued

•A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.

•A subclass in a different package can only override the non-final methods declared public or protected.

•Constructors cannot be overridden.

Page 15: Chapter 05 polymorphism

Rules for method Overloading•Parameter sending should be unique

because the compiler only know the methods during compile-time only

•Also known as static/early binding polymorphism compared to overriding which is a dynamic binding

Page 16: Chapter 05 polymorphism

Examples of overloadingPublic class Person {

private String firstname;private String lastname;

public Person(){firstname=“”;}

public Person(String lname){firstname=“”;

lastname=lname;}

public Person(String fname,String lname){firstname=fname;

lastname=lname;}

Page 17: Chapter 05 polymorphism

//call the object in main function

Person p1 = new Person();Person p2 = new Person(“Chu");Person p3 = new Person(“Rizal", “Man");