Inheritance
.(method name)() has precedence so do
( (cast) object name).(method name)()
2 things to do to group a chaotic mess of classes
1st: natural order based on what you know --> group by type works the best 2nd: duplicate code
creating an ArrayList
ArrayList<E> name = new ArrayList<>();
UML diagram
Unified Modeling Language name in top box what the method does (accessible variables and methods) in the bottom box
You don't have to explicitly put in a super command when
a no-argument constructor exists in the superclass think of adding super like building a constructor with the constructor of the child class
subclass may not have
access to everything
abstract setting means that
an object will not be created from that class, but laws of inheritance still applyB
most of the times, which methods are to be used
are determined at compile time, including cases with overloading known as static or early binding
every class is a child in java
as it originates from the object class --> even if it is not explicilty defined
finals are public
b/c they are final
you set instance variables to public
because otherwise, only that class will have direct access to them. The other classes still inherit them, but won't have direct access to them. private means that only that specific class has access to that private thing. it inherits in the superclass part of the created class, but it is like it is in a locked room
IS-A only works from child to parent
because that makes sense and is not ambiguous as parents can have multiple children. // may work in one case, but its need to be generic. transitve prop is at play --> look at example on page 2 b/c of trans prop --> indirect parent hierarchy comes into play
java automatically inserts super(); in some cases
but in other cases, you have to explicitly define it. super runs the no argument constructor of the superclass that line basically tells the java to construct the superclass's objects
abstract classes do not need abstract methods
but, abstract methods need to be in an abstract class
parent
child
basic principle
define a new class based upon an existing class
base
derived
you often use
diagrams
protected works like private
except it gives direct access in the case of inheritance
implements are good
for holding constants
Look at object creation featuring diff classes
in packet child on right side
you normally want to be explicit but
in the case of interface, you don't have to
implements are a more extreme version of abstract
interface instead of class no instance variables or methods with bodies. no objects come form it either child methods are not a child but follow laws of inheritance and are req to override (link thru using implements rather than extends and implements instead of abstract) so can only extend once, but can implement many times (how much ever it wants) extend then implement (you can separate implements using da comma)
in other cases, which methods are used won't be known till run-time
like in the cases of user input or creating an object with diff called dynamic or late binding user input reveals why having diff class names is useful
Child/parent hierarchy can get flavorful
look at top of page 3 inheritance brings cohesion and concision cohesion is there to add structure
Java uses single inheritance
meaning that there is only one parent per child. Some use multiple inheritance, like humans, but that gets problematic.
making an abstract method
means no body because that does not make sense
method overriding
names and parameters are the same can't do it in the same class, but can do it with inheritance you override a superclass method in the subclass
Inheritance is between classes
not objects
Use a downcast to fix the prob of
obj ref referring to a diff object than the creation. works just like cast in reference to numerical data types you can only use if you have done the object creation line featuring child and parent. You can't do it out of the blue. you can only downcast a subclass, not a superclass
Object is the
overlord of all Java classes always sits on top of the hierarchal tree, it is just not mentioned so all classes inherit instance variables, constants, and methods from the Objects class.
setting a class to abstract
public abstract class (name)
syntax is
public class ChildClass extends ParentClass { ChildClass specifics } look at naming convention when studying --> but UpperCamel fo sho also look at creating objects/classes --> and what an instance varible is instance variables declared globally are non-static everything globally should normally be private except finals, but should be protected when working with classes for objects.
Polymorphism
refers to any method that has been overridden
superclass
subclass
Each subclass can only have one
superclass
super creates a construction chain/order that starts with Objects
that how the heap layers build up
in a uml diagram
the arrow goes from child to parent backwards english --> child IS-A parent // hyphen must be there the link between child and parent is a IS-A link (use it this way even when AN is proper english)
in constructors of child classes
the first line of the constructor must be a super command
for methods overriding
the shortcut to get superclass's method is to use super.(method name)() then add on. do this for 2 reasons a). shortens the amount of code you actually write b). changes to the superclass automatically reflect in the super call in the subclass if there is method 2 layers up for example you can do super.super.(method name)(). by doing this, java is not running that superclass, instead it is copying that code to you current subclass above point rears its head in cases of overridden methods/variables2
How do you give indirect access
through using accessor and mutators.
method overloading
two methods with the same name but diff parameters
when creating an object that is a subclass of another
you are building up layers of code on the heap --> look in notes the whole superclass in constructed but the subclass inherits everything but the constructors // remember, speak of it in classes not objects. subclass wraps around the superclass
implement classes
you can only create constants and since java knowns this you can treat it like a normal variable (dont need public (notice public setting) static final) but remember to capitalize name
by setting a method to abstract
you require the method to be overridden in each and every non-abstract child class