Chapter 05 polymorphism

589 views
538 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
589
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • CLASS EMPLOYEE IS GENERIC CLASS OF ALL EMPLOYEE
  • CLASS EMPLOYEE IS GENERIC CLASS OF ALL EMPLOYEE
  • CLASS EMPLOYEE IS GENERIC CLASS OF ALL EMPLOYEE
  • Chapter 05 polymorphism

    1. 1. Chapter 5 : Polymorphism<br />
    2. 2. Objectives<br />Student will learn about:<br />overriding<br />overloading<br />
    3. 3. Polymorphism<br />
    4. 4. PolymorphismPrinciple<br />
    5. 5. Example<br />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<br />Subclassess of class can define their own unique behaviors but still share some of the same functionality of the parent class<br />
    6. 6. Types of polymorphism<br />Overriding<br />Methods of a subclass overwrite methods in superclass<br />E.g the method in subclass using the same name, same return type<br />Run-time polymorphism<br />Overloading<br />Methods of subclass having same name but different signatures<br />E.g the method in subclass using the same name but different parameter list, return type<br />Compile-time polymorphism<br />
    7. 7. Example<br />VEHICLE<br />CAR<br />BUS<br />
    8. 8. ~ continued<br />public class Vehicle<br />{ <br />public int gear; <br />public int speed; <br />public Vehicle(intstartSpeed, intstartGear) <br />{ gear = startGear; <br />speed = startSpeed; } <br />public void setGear(intnewValue) { gear = newValue; } <br />public void applyBrake(int decrement) { speed -= decrement; }<br />public void speedUp(int increment) { speed += increment; }<br />public void printDescription(){ System.out.println("nVehichle is in gear " + this.gear + " and travelling at a speed of " + this.speed + ". "); }<br /> } <br />
    9. 9. public class Car extends Vehicle<br />{ <br />Private String suspension;<br />public Car (intstartSpeed, intstartGear,inttypeSuspension) <br />{ <br />super(startSpeed,startGear);<br />setSuspension(typeSuspension);<br />}<br />Public String getSuspension(){return suspension;}<br />Public void setSuspension(String typeSuspension){suspension=typeSuspension}<br />public void printDescription(){ <br />Super.printDescription();<br />System.out.println("nThe car has " + this.getSuspension() + “ suspension. "); }<br /> } <br />
    10. 10. public class Lorry extends Vehicle<br />{ <br />Private String width;<br />public Lorry (intstartSpeed, intstartGear,inttyrewidth) <br />{ <br />super(startSpeed,startGear);<br />setTyreWidth(tyrewidth);<br />}<br />Public String getTyreWidth(){return width;}<br />Public void setTyreWidth(String tyrewidth){width= tyrewidth}<br />public void printDescription(){ <br />Super.printDescription();<br />System.out.println("nThe Lorry has " + this. getTyreWidth() + “ inch tyres. "); }<br /> } <br />
    11. 11. Executes the code<br />public class TestVehicle<br />{ <br /> public static void main(String[] args)<br /> { <br />Vehicle vehicle1, vehicle2,vehicle3;<br /> vehicle1 = new vehicle1(20, 10); <br /> vehicle2 = new Car(20, 10, "Dual"); <br /> vehicle3 = new Lorry(40, 20, 23); <br /> vehicle1.printDescription(); <br /> vehicle2.printDescription(); <br /> vehicle3.printDescription(); <br /> } <br />}<br />
    12. 12. The outputs<br />Vehicle is in gear 20 and travelling at a speed of 10.<br />Vehicle is in gear 20 and travelling at a speed of 10.<br />The car has Dual suspension.<br />Vehicle is in gear 40 and travelling at a speed of 40.<br />The Lorry has 23 inch tyres.<br />
    13. 13. Rules for method overriding<br />The argument list should be exactly the same as that of the overridden method.<br />The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.<br />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.<br />Instance methods can be overridden only if they are inherited by the subclass.<br />A method declared final cannot be overridden.<br />A method declared static cannot be overridden but can be re-declared.<br />If a method cannot be inherited then it cannot be overridden.<br />
    14. 14. ~continued<br />A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.<br />A subclass in a different package can only override the non-final methods declared public or protected.<br />Constructors cannot be overridden.<br />
    15. 15. Rules for method Overloading<br />Parameter sending should be unique because the compiler only know the methods during compile-time only<br />Also known as static/early binding polymorphism compared to overriding which is a dynamic binding<br />
    16. 16. Examples of overloading<br />Public class Person {<br /> private String firstname;<br /> private String lastname;<br /> public Person(){<br />firstname=“”;}<br /> public Person(String lname){<br />firstname=“”;<br />lastname=lname;}<br />public Person(String fname,Stringlname){<br />firstname=fname;<br />lastname=lname;}<br />
    17. 17. //call the object in main function<br />Person p1 = new Person();Person p2 = new Person(“Chu");Person p3 = new Person(“Rizal", “Man");<br />

    ×