Java Inheritance
Method Overloading
- Java allows multiple methods with same name, but different parameters
Interfaces & Classes
- Interface is a specification of what a List is able to do, not how to do it
public interface List61B<Item> {
public void addFirst(Item x);
public void addLast(Item y);
public Item getFirst();
public Item getLast();
public Item removeLast();
public Item get(int i);
public void insert(Item x, int position);
public int size();
}
- use the implements keyword to tell Java compiler that SLList and AList are hyponyms of List61B
public class AList<Item> implements List61B<Item> {
}
Overloading vs Overriding
Overriding
- if a subclass has a method with the exact same signature as in the superclass => override
Overloading
- methods with the same name but different signatures are overloaded
public void makeNoise()
public void makeNoise(Dog x)
public int abs(int a)
public double abs(double a)
-
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
-
No. C++ and Java both disallow overloading on a functions’s return type.
Notation
- adding the @Override notation
- with this tag, the code won’t compile if it is not actually an overriding method => protection against typos
- optional
Interface Inheritance
- specifying the capabilities of a subclass using the implements keyword is known as interface inheritance
- interface: the list of all method signatures
- subclass must override all of those methods!
- otherwise will fail to compile
Implementation Inheritance
-
interface inheritance
- subclass inherits signatures, but NOT implementations
-
Allow implementation inheritance
- subclasses can inherits signatures AND implementations
-
use default keyword to specify a method that subclasses should inherit from an interface
if X is a superclass of Y, then an X variable can hold a reference to a Y
Static and Dynamic Type
- static type (compile-time type): specified at declaration
- dynamic type (runtime type): specified at instantiation (when using new)
- equal to the type of the object being pointed to
Dynamic Method Selection for Overriden Methods
- if Y overrides the method, Y’s method is used instead
public interface Animal {
default void greet(Animal a) {
print("hello animal");
}
default void sniff(Animal a) {
print("sniff animal");
}
default void praise(Animal a) {
print("u r cool anima");
}
}
public class Dog implements Animal {
@Override
void sniff(Animal a) {
print("dog sniff animal");
}
void praise(Dog a) {
print("u r cool dog");
}
}
Animal a = new Dog();
Dog d = new Dog();
a.greet(d); // "hello animal"
a.sniff(d); // "dog sniff animal"
d.praise(d); // "u r cool dog"
a.praise(d); // “u r cool animal”
praise is OVERLOADED!!! NOT OVERRIDEN!!!
Implementation Inheritance: Extends
- because of extends, subclass/derived class inherits all members of the parent class:
- all instance and static variables
- all methods
- all nested class
- except private members
- constructors are not inherited
- Java will automatically call super() for you
- but you have to call super(x) with argument yourself