OVERRIDE VS OVERLOAD
override eg: when the interface contain this method ,you implements it and try to override the method for a new purpose
1 |
|
overload eg: the same named method but contains different parameter
1 | public static void peek(List61B<String> list) { |
dynamic method selection
1 | LivingThing lt1; |
A Trap!!!!
This is a overload not the different class override
1
2
3
4
5
6
7 SLList<String> SP = new SLList<String>();
List61B<String> LP = SP;
SP.addLast("elk");
SP.addLast("are");
SP.addLast("cool");
peek(SP);
peek(LP);
- Interface inheritance (what): Simply tells what the subclasses should be able to do.
- EX) all lists should be able to print themselves, how they do it is up to them.
- Implementation inheritance (how): Tells the subclasses how they should behave.
- EX) Lists should print themselves exactly this way: by getting each element in order and then printing them.
Implementation inheritance is a relationship where a child class inherits behaviour implementation from a base class.
Interface inheritance is when a child class only inherits the description of behaviour from the base class and provides the implementation itself.
Implementation inheritance may sound nice and all but there are some drawbacks:
- We are fallible humans, and we can’t keep track of everything, so it’s possible that you overrode a method but forgot you did.
- It may be hard to resolve conflicts in case two interfaces give conflicting default methods.
- It encourages overly complex code
extends and implements
the extends
keywords defines an “is-a” relationship between a subclass and a parent class
1 | public VengefulSLList() { |
1 | public VengefulSLList(Item x) { |
notice: you can omit the super when you call the no parameter method but if you call the method with parameter you must call the right super(x)with the right parameter.
Casting
1 | Poodle frank = new Poodle("Frank", 5); |
Interface and abstract class
We’ve seen interfaces that can do a lot of cool things! They allow you to take advantage of interface inheritance and implementation inheritance. As a refresher, these are the qualities of interfaces:
- All methods must be public.
- All variables must be public static final.
- Cannot be instantiated
- All methods are by default abstract unless specified to be
default
- Can implement more than one interface per class
Below are the characteristics of abstract classes:
- Methods can be public or private
- Can have any types of variables
- Cannot be instantiated
- Methods are by default concrete unless specified to be
abstract
- Can only implement one per class