i. Override – when a subclass implements a
method that is also defined in a super class
1. Examples – toString(), equals(Object obj)
ii. Overload – when a method shares a name with
another in the same class, but has different parameters (making it an entirely
unique method)
1. Examples – random.nextInt(),
random.nextInt(int range)
i. Method – super.<method>();
ii. Constructor – super();
i. Remember – a class can only extend one other
class.
ii. But, if class C extends class B, class B
either extends another class, or Object, and so forth
1. Direct vs indirect superclass
iii. Cyclic class inheritance error – for
instance, A extends B, B extends C, C extends A
iv. Thus, Object is a superclass for every class
– top of the tree, so to speak
i. Contains very general methods, such as
toString and equals
ii. Default behavior for these methods:
1. toString – Class@Address
2. equals – compares Addresses
iii. We will often override these methods with
more specific implementations.
iv. Even though we should override these methods
before using, they serve a purpose, and there’s a reason we override them,
instead of making new ones
1. They are a ‘contract’ in a sense, with all
subclasses of object, that they are a valid method name that does something
consistent
2. So, we know that we can call toString from a
object of any class, or equals, and we know how to use it, even if we don’t
know a thing about the implementation of it
i. Remember that we can store a subclass object
in a superclass variable?
Example – Object o=new Random();
o instanceof Random ->true
ii. This is why the instanceof operator is
important – it tells us whether the object stored in the variable is the
desired type.
iii. Useful when converting from superclass to
subclass object
1. We would do this if we want to call subclass
methods from an object that was declared generically
2. Don’t use instanceOf to bypass polymorphism –
that will be handled already if classes are implemented well
i. Another level of abstraction – skeleton for
code
ii. Provides generality to code
i. The only other type we’ve seen before this is
Class
i. A set of methods
ii. Their signatures (params, return, etc)
i. Leaving ‘stub’ methods is ok, though
i. Methods in an interface are abstract ->
have name, params, and return type, but don’t have implementation
ii. All methods are public
iii. No instance variables
iv. No static methods
public interface
<name>{
<returnType>
<methodName>(<params>);
…
}
i. Do not put any code in methods
ii. Do not say methods are public – they always
are
iii. No instance variables
iv. No static methods
v. Pay attention to the semicolon after the
method declaration(s).
<type> <CONSTANT_NAME>=<value>;
i. Note – these are always public static final,
even though we don’t explicitly say so
i. Like extends, Class implements Interface
i. Reminder again – stubs are ok
<InterfaceName> o=new <ClassThatImplementsInterface>();