Unit 2 Object Oriented Programming-Inheritance
Unit 2 Object Oriented Programming-Inheritance
Unit 2 Object Oriented Programming-Inheritance
Car
class First {
public First() { System.out.println(“ First class “); }
}
public class Second extends First {
public Second() { System.out.println(“Second class”); }
}
public class Third extends Second {
public Third() {System.out.println(“Third class”);}
}
First class
Second class
Third class
RetailBusiness ServiceBusiness
• int i = 5
double d = 5.0
Why overload a method?
• So you can use the same names for methods that do essentially the same
thing
– Example: println(int), println(double), println(boolean), println(String), etc.
• So you can supply defaults for the parameters:
int increment(int amount) {
count = count + amount;
return count;
}
int increment() {
return increment(1);
}
– Notice that one method can call another of the same name
• So you can supply additional information:
void printResults() {
System.out.println("total = " + total + ", average = " + average);
}
void printResult(String message) {
System.out.println(message + ": ");
printResults();
}
Overriding
• class Animal
• { • This is called
public static void main(String args[])
• {
overriding a method
Animal animal = new Animal();
Dog dog = new Dog(); • Method print in Dog
animal.print();
dog.print();
overrides method
}
void print()
print in Animal
• { • A subclass variable
System.out.println("Superclass Animal");
} can shadow a
}
• public class Dog extends Animal superclass variable,
• { but a subclass
void print()
• {
method can override
}
System.out.println("Subclass Dog");
a superclass method
}
How to override a method
• Create a method in a subclass having the same
signature as a method in a superclass
• That is, create a method in a subclass having the
same name and the same number and types of
parameters
– Parameter names don’t matter, just their types
• Restrictions:
– The return type must be the same
– The overriding method cannot be more private than the
method it overrides
Why override a method?
• Dog dog = new Dog();
System.out.println(dog);
– Prints something like Dog@feda4c00
– The println method calls the toString
method, which is defined in Java’s top-level
Object class
• Hence, every object can be printed
(though it might not look pretty)
• Java’s method public String toString()
can be overridden
Why override a method?
• If you add to class Dog the following:
public String toString() {
return name;
}
Then System.out.println(dog); will print the
dog’s name, which may be something like:
Fido
Calling an overridden method
• When your class overrides an inherited method,
it basically “hides” the inherited method
• Within this class (but not from a different class),
you can still call the overridden method, by
prefixing the call with super.
– Example: super.printEverything();
• You would most likely do this in order to observe
the DRY principle
– The superclass method will do most of the work, but
you add to it or adjust its results
– This isn’t a call to a constructor, and can occur
anywhere in your class (it doesn’t have to be first)
Overloading vs. Overriding
• Don't confuse the concepts of overloading and overriding
• Overloading deals with multiple methods with the same
name in the same class, but with different signatures
• Overriding deals with two methods, one in a parent class
and one in a child class, that have the same signature
• Overloading lets you define a similar operation in different
ways for different data
• Overriding lets you define a similar operation in different
ways for different object types
Dynamic Binding
• Binding: linking between messages and
methods
class Math {
• abstract classes
– may have both implemented and non-
implemented methods
sample abstract class
abstract class TwoDimensionalGeoFigure {
public abstract double area();
public abstract double perimeter();
public abstract void printInfo();
public void setOutlineColor(Color cc) {
// code to set the color
}
public void setInsideColor(Color cc) {
// code to set the color
}
}
The Object Class
• A class called Object is defined in the java.lang
package of the Java standard class library
• All classes are derived from the Object class
• If a class is not explicitly defined to be the child of an
existing class, it is assumed to be the child of the
Object class
• Therefore, the Object class is the ultimate root of
all class hierarchies
The Object Class
• The Object class contains a few useful methods,
which are inherited by all classes
• For example, the toString method is defined in the
Object class
• Every time we have defined toString, we have
actually been overriding an existing definition
• The toString method in the Object class is defined
to return a string that contains the name of the object’s
class together along with some other information
The Object Class
• The equals method of the Object class returns true
if two references are aliases
• We can override equals in any class to define equality
in some more appropriate way
• The String class (as we've seen) defines the equals
method to return true if two String objects contain
the same characters
• Therefore the String class has overridden the
equals method inherited from Object in favor of its
own version
Reflection
• What exactly is a class?
• It’s a collection of different things, such as:
– Fields
– Methods
– Constructors
Client
implements Proxy
By way of
Factory
Generates or obtains
Creating a proxy
• Things you need
– A ClassLoader
– An array of interfaces to implement
– An InvocationHandler
• What’s the InvocationHandler?
– Basically, a single method. Could do anything
– Doesn’t usually implement any essential business logic
– Dispatches to an underlying service, possibly after (or before)
providing some additional service.
– Note: underlying service may itself be a proxy!
Proxy Properties
• Can be cast to any interface used in creating the proxy class.
• instanceof works as expected
• Proxy.isProxyClass() is true for the class
• Class name is undetermined, but will probably be something
like Proxy$1.
• Method invocation is encoded and dispatched to the
handler’s invoke method.
• Is Serializable
– This is huge!
– Implications for RMI, JNDI, J2EE