Download presentation
Presentation is loading. Please wait.
1
January 31 Is-a Versus Has-a Polymorphism Abstract & Final Class/Method
2
Is-a versus Has-a zDon’t confuse inheritance with having a data field that is another class(composition) zDeclaring a class inside another just sets up a reference variable to the class with no relationship zThe difference between Inheritance and Composition becomes clear by asking Is-a ? Or Has-a ? question zWhat would you say, a car has an engine or a car is an engine? zWe would say a Manager is an Employee.
3
Extended Class Is-a Super class type Class A method a1() method a2() Class B method b1() method b2() extends … B b = new B(); b.a1(); // OK!!! B.b2(); // OK!!! A a = new A(); a.b2(); // Not OK!!! A is not same type as B
4
No Multiple Inheritance zMultiple inheritance means having more than one immediate super class. class FlyingMachine class FloatingVessel class SeaPlane zSorry, This is not allowed in Java!!! zMultiple inheritance may give us total mess. Imagine two super class have the same method with totally different behavior.
5
Rationale Behind Inheritance(subclassing) zClass Reuse. Inheritance inherently provides class reuse. zProvides logical way of implementing generic type handling. What if you want to treat different types of objects as one common type? void promote(Employee worker) { … } promote() can take in any objects of Employee Manager or Executive types. zPolymorphism is for ensuring individuality of each subclass.
6
Polymorphism(Name Reuse) zOverloading (easy and weak polymorphism) In any class, we can use the same name for several different methods. zOverriding A subclass class can have a method with the same signature as a method in the superclass. zOverloading is resolved at compile time. zOverriding is resolved at runtime. What does this mean?
7
Upcasting & Overriding zUpcasting is always safe and allowed. Manager manager = new Manager(“hoony,”2m”,”12/1/00”); Employee employee = manager; zNow manager is upcasted to Employee, consider employee.raiseSalary(0.5); Which raiseSalary() will be invoked, that of Manager or Employee? zThe real type is resolved at runtime.Late-binding True Beauty of Polymorphism!!! manager is created as Manager Type!!!
8
Forcing Overriding Off: Final zfinal class means “No one can extend this class!!!” Ex) java.lang.Math public final class Math {... public static native double sin(double a); zSimilarly, final method means “No subclass can override this method!!!”
9
Forcing Overriding: Abstract zabstract class means “I am incomplete, please extend me by overriding all my abstract methods!!!” zabstract method has no body. Its purpose is to force some subclass to override it and provide a concrete implementation of it. zabstract class has zero or more of its methods are abstract.
10
abstract class Shape { public void setColor(Color color) { … } public void setVisible(boolean t) { … } public abstract void draw(); } class Circle extends Shape { public void draw() { // draw circle here!!! } abstract class Trapezoid extends Shape { private int sides = 4; public abstract void draw(); } class Rectangle extends Trapezoid { public final void draw() { // draw four sides using this // method only!!! } class Square extends Rectangle { public void void draw() { // Sorry!!! This overriding is not // Allowed!!! }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.