Objects & Classes

Download Report

Transcript Objects & Classes

Inheritance
Lakshmish Ramaswamy
Overriding and Overloading
• Method overriding requires same method
signature & same return type;
– original method inherited from its superclass
• Overloading requires different method
signatures, but method name is same
– parameters must be different in type and/or number
• Method can be overloaded in same class or
subclass
• Overriding and overloading can used in
conjunction
Compatibility of Array Types
• Inheritance for aggregate types poses problems
person [ ] arr = new employee [5];
student s = new student(…);
arr[0] = s;
• Code compiles
• But arr[0] is referencing an employee but student
IS-NOT-A employee
• Each array keeps track of objects it is supposed
to store
• JVM will throw an ArrayStoreException if
incompatible type is inserted into array
Covariant Return Types
• Prior to Java 5 overridden methods in
subclass were required to have same
return type as that of superclass method
• Since Java 5 the return type of overridden
method only needs to be type compatible
with the base class method
public person makeCopy(); // in person class
public employee makeCopy(); // in employee subclass
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; }
Example contd.
• Need type declaration
of WhatType
Shape
– Say “Shape”
• Inherit Circle and
Rectangle from Shape
• Shape needs to have
area method
• But, how to implement
area method in Shape?
Circle
Rectangle
Square
A Possible Implementation
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
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
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
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
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”
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
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