Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.

Similar presentations


Presentation on theme: "Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles."— Presentation transcript:

1 Inheritance Lakshmish Ramaswamy

2 Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles Calculating the total area public static double totalArea (WhatType [ ] arr){ double total = 0.0; for (int i = 0; i < arr.length; i ++){ if(arr[i] != null) total += arr[i].area()} return total; }

3 Example contd. Need type declaration of WhatType –Say “Shape” Inherit Circle and Rectangle from Shape Shape needs to have area method But, how to implement area method in Shape? Shape Circle Rectangle Square

4 A Possible Implementation

5

6

7 Pros and Cons Advantages –New classes can be added to hierarchy without changing existing implementations –No instanceof tests –No casting necessary Disadvantage –What if someone creates a Shape object and assigns to an array location? arr [5] = new Shape(); –Wrong results from totalArea unless additional tests are performed

8 What is Really Happening? Area method is a placeholder –Not to be called directly –Programmer is trying to signal an error –Just there so that the compiler does not complain In fact the Shape class itself is a placeholder –Common superclass for other classes –An abstract concept

9 Abstract Methods & Classes A method that just declares the functionality Does not provide a default implementation All derived class objects must eventually implement –Each object needs to provide its own implementation Shape class “declares” the area method –Must be implemented in Circle and Rectangle

10 Abstract Class Class having at least one abstract method Cannot construct an abstract class –Compiler will check for violations Might declare one or more constructors Might also provide implementations of constructors –Will be used when derived classes use super Can have non-abstract methods

11 Abstract Class (contd.) Can declare both static and non-static fields All abstract classes must be explicitly declared in Java A derived class that does not implement an abstract method remains abstract –Hence, they must be declared “abstract”

12 Shape as abstract class Shape a, b, c, d; a = new Circle (3.0); b = new Square (4.0); c = new Rectangle (3.5, 4.5); d = new Shape (); // illegal

13 Methods Summary Final methods – Cannot be overridden in derived classes –VM may perform inline optimization at runtime Abstract method – Overriding resolved at runtime using dynamic dispatch Static method – Resolved at compile time as there is no controlling object Normal methods – Resolved at runtime using dynamic dispatch

14 Multiple Inheritance A class being derived from more than one base class –Ex: A StudentEmployee Class being derived both from Student and Employee Some OO languages (ex: C++) support multiple inheritance Java does not allow multiple inheritance

15 Issues with Multiple Inheritance Does StudentEmployee get two copies of name, address and phone number? If StudentEmployee does not override toString method, which implementation would be used? If a method is declared to be final in one base class and not in the other, would the StudentEmployee be allowed to override the method?

16 Interface Alternative to multiple inheritance The ultimate abstract class Consists only of public abstract methods & public static final fields –No implementation of any method Class providing definitions of all abstract methods is said to implement the interface Multiple interfaces do not suffer from same problems as multiple inheritance Class can implement multiple interfaces but extend from a single class

17 Specifying Interface Similar to a class declaration but uses the keyword interface Lists methods that need to be implemented Class implementing an interface should do two things –Declare that it implements the interface using “implements” – Provide implementations of all methods declared in interface A method implementing an interface may remain abstract

18 Example of Interface

19 Example of Implementing Class

20 Multiple Interfaces Java allows multiple interfaces List all the interfaces a class is implementing Provide implementations of all methods in all interfaces public class X implements a, b { … }


Download ppt "Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles."

Similar presentations


Ads by Google