1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Interface
groups declarations of similar capabilities of different classes together, declare but do not define their methods, cannot be instantiated, formal contracts and ensure consistency
model an “acts-as” relationship
implement
key work in class call to connect to interface, contracts that classes agree to to define all the methods declared in said interface
@Override
put above method signature, an annotation - a signal to the compiler (and to anyone reading the code), allows compiler to enforce that interface actually has method declared
method signature
states the public/private, return type, nameOfMethod, and the parameter type and parameter name is there is one
Polymorphism
direct definition: “many forms”
a way of coding generically; reference instances of classes that share one or more methods as one generic type
treats multiple classes as their generic type while still allowing specific method implementations for specific subclasses to be executed
eg. cars and bikes can both move() → refer to them as classes of type Transporter
es. Transporter durusCar = new Car();
Actual type
part of polymorphism, ensures specificity
the class that the Java compiler will look for the definition of any methods, even though they are considered instances of the type of the interface
Declared type
can reference many classes using one generic type
compiler will limit any caller so it can only call methods on instances that are declared as instances of interface type AND only the methods are declared in that interface
es. move() in the Transporter interface is the only method called
Inheritance
models an “is-a” relationship, a way of modeling very similar classes and facilitating code reuse
hierarchical relationship, assures that all subclasses of a superclass will have the superclass’ public capabilities (i.e., code) automatically – no need to re-specify
eg. Poodle is a Dog, but not every Dog is a Poodle
Superclass/Parent Base
A class that is inherited from
Subclass/child/derived
A class that inherits from another, can only inherit from one superclass
inherits all its parent’s public capabilities
subclass extension
extends superclass by adding new methods (the subclass should define specialized methods)
overriding inherited methods
defining abstract methods (superclass declares but does not define all methods)
extends
means “is a subclass of” or “inherits from”, lets the compiler know that subclass is inheriting from superclass,
eg. extends <superclass name>
Partially Override Methods
partially accept the inheritance relationship
Step 1: use @override to signal to compiler
Step 2 : declare method with same name and return type as super class
Step 3: refer to parent class method using super.<methodName>, signals to invoke original inherited method
Step 4: add something specialized
lowest common denominator
in the hierarchical system, the most recent superclass that all the subclasses have in common
super()
parent’s constructor,
called from the subclasses’s constructor to make sure the superclass’s instance variables are initialized properly
allows us to access to superclasses private instance variables
only called once, first line of subclass’s constructor
abstract method
declared in the superclass, but not defined - it is up to the subclasses to father down hierarchy to provide their own implementations
used when subclasses can’t really re-use any implementation that the superclass provides
eg. all Cars should loadPassengers, but each subclass will loadPassengers very differently
use @Override
eg. public abstract void loadPassanger();