0%

CS-61B

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
2
3
4
@Override
public void addFirst(Item x) {
insert(x, 0);
}

overload eg: the same named method but contains different parameter

1
2
3
4
5
6
7
8
public static void peek(List61B<String> list) {
System.out.println(list.getLast());
}
public static void peek(SLList<String> list) {
System.out.println(list.getFirst());
}


dynamic method selection

1
2
3
4
5
6
LivingThing lt1;

lt1= new Fox();

Animal a1=lt1;

image-20230419133109776

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
2
3
4
public VengefulSLList() {
//super();
deletedItems = new SLList<Item>();
}
1
2
3
4
public VengefulSLList(Item x) {
super(x);
deletedItems = new SLList<Item>();
}

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.

image-20230420133808622

Casting

1
2
3
4
Poodle frank = new Poodle("Frank", 5);
Malamute frankSr = new Malamute("Frank Sr.", 100);

Poodle largerPoodle = (Poodle) maxDog(frank, frankSr); // runtime exception!

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