Inheritence, Abstract Classes, Interfaces
implement an interface
- *implements* keyword in class header - provide all methods specified by interface *contract, when class implements interface it must stick to contract
polymorphism
- ability to take many forms - parameters can accept arg. polymorphically ex: reference variable *shapeshifter
Object class (Java API)
- all other classes directly or indirectly inherit from this class - when *extends* keyword isn't used to inherit from another class, Java automatically extends from Object class - every class inherits Object class's members (toString and equals method) - *toString method* returns reference to String containing object's class name, then @ and object's hash code - *equals method* accepts reference to an object as argument and returns true if argument references calling object - if you want to change behavior of either of these methods for a given class, must override them in the class
inheritence
- allows new class to extend existing class - new class inherits members of class it extends - involves super class & subclass - can only inherit public members
abstract method
- appears in superclass - must be overriden in subclass - only header, no body, *ends* w/ *;* - ensures subclass implements method - can't create instance of class - can't be instantiated ex: public abstract void setValue(int value);
implementing multiple interfaces
- class can only extend 1 superclass - multiple interfaces can be implemented, must provide methods specified by all of them
instanceOf
- figure out if object is instance of certain class - boolean ex: refVar instanceof ClassName
parent class, superclass, base class
- general class - can't class any of the subclass methods
overloading
- if 2 methods have same name, but different signatures - can work for when methods are in the same class or when 1 method is in a superclass and other is in subclass
"is a" relationship
- inheritance used to create this among classes - specialized object has all characteristics of general object + unique characteristics
default method
- interface method w/ body - possible for class to override default method - allow add + new methods to existing interface w/o causing errors
method signature
- method's name & data types of method's parameters, in same order as they appear
protected members of class
- only can be accessed by methods in subclass - same type of thing as private or public access specifications - can be directly accessed by methods of same class or methods of subclass
*super* keyword
- refer's to object's superclass - can be used to call superclass constructor - can be used to access members of superclass
interface reference variable
- reference any object that implements that interface - can't create instance of interface
realization relationship
- relationship between class and interface - class realizes interfaces
abstract class
- serves as superclass for other classes - other classes extend it - can't be instantiated
child class, subclass, derived class
- specialized class - extended version of superclass - variables and methods w/o being rewritten - new things can still be added to the subclass - when *object* is created of subclass, all of the members declared in the subclass and superclass can be accessed
interface
- specifies behavior for class - like a class that contains only abstract methods - can't be instantiated - when class implements interface, must override methods specified by interface - all methods *public* << interface >> in UML header
overriding
- subclass method can use the same name as superclass method & same parameters (unlike overloading) b/c sometimes the specialized class needs to change the generic method from superclass - aka overriding the superclass method * not same thing as overloading @Override should be placed right above subclass method that it is overriding
how do constructors work in inheritance relationships?
- superclass constructor always executes before subclass constructor
preventing a method from being overriden
- when method declared w/ *final* modifier, can't be overriden in subclass
overriding vs overload
- when subclass *overloads* a superclass method, both methods may be called w/ subclass object - when subclass *overrides* superclass method, only subclass's version of method can be called w/ a subclass object
fields in interfaces
-can have in interface - treated as *final* and *static* - must provide initialization value - any class that implements field has access to variables
guidelines for call superclass constructor
1. *super* statement can *only* be written in subclass's constructor 2. *super* statement must be 1st statement in subclass's constructor 3. if subclass constructor doesn't clearly call superclass constructor, then automatically superclass's default constructor or no-arg constructor = super(); - = same thing