Java Object Orientation 5 Min Drill
Encapsulation nothing but kept instance variables with the private modifier and provide access through getter and setter.
IS-A refers to inheritance or implementation i.e. extends or implements.
HAS-A means an instance of one class "has a" reference to an instance of another class or another instance of the same class.
All classes are subclasses of type Object and therefore by default inherit Object's methods.
Polymorphism means "many forms"
A reference variable is always of a single, unchangeable type, but it can refer to a subtype object.
A single object can be referred to by reference variables of many different
types as long as they are the same type or a supertype of the object.
The reference variable's type (not the object's type) determines which methods can be called!
PPolymorphism applies to overriding, not to overloading.
Rules for overriding a method is
Rules for Overloaded method is
- If a method can't be inherited, you cannot override it.
- The argument list must exactly match that of the overridden method. If they
don't match, you can end up with an overloaded method you didn't intend.
- The return type must be the same as, or a subtype of, the return type declared
in the original overridden method in the superclass.
- The access level can't be more restrictive than that of the overridden method.
- The access level CAN be less restrictive than that of the overridden method.
- Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package
as the instance's superclass can override any superclass method that is not marked private or final.
- A subclass in a different package can override only those nonfinal methods marked public
or protected (since protected methods are inherited by the subclass).
- The overriding method CAN throw any unchecked (runtime) exception,
regardless of whether the overridden method declares the exception.
- The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method.
e.g. Method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException,
Exception, or any other nonruntime exception unless it's a subclass of FileNotFoundException.
- Overriding method can throw narrower or fewer exceptions.
- You cannot override a method marked private/final.
- You cannot override a method marked static.
Object type (not the reference variable's type) determines which overridden method is used at runtime.
Reference type determines which overloaded method will be used at compile time
Downcasting - If you have a reference variable that refers to a subtype object,
you can assign it to a reference variable of the subtype. You must make an explicit cast to do this,
and the result is that you can access the subtype's members with this new reference variable.
Upcasting - You can assign a reference variable to a supertype reference variable explicitly
or implicitly. This is an inherently safe operation because the assignment restricts the access capabilities of the new variable.
Overloaded methods can change return types; overridden methods cannot, except in the case of covariant returns.
Object reference return types can accept null as a return value.
Array is a legal return type, both to declare and return as a value.
methods with primitive return types, any value that can be implicitly
converted to the return type can be returned.
Methods with an object reference return type can return a subtype
- Overloaded methods MUST change the argument list.
- Overloaded methods CAN change the return type.
- Overloaded methods CAN change the access modifier.
- Overloaded methods CAN declare new or broader checked exceptions.
- Method can be overloaded in the same class or in a subclass.
- Methods from a superclass can be overloaded in a subclass
Static init blocks run only once, when the class is first loaded. Multiple blocks run from the top down.
Normal init blocks run for every new instance, right after all the super constructors have run.
Multiple blocks run from the top of the class down.
Constructor is always invoked when a new object is created.
Each superclass in an object's inheritance tree will have a constructor called.
Every class, even an abstract class, has at least one constructor.
Constructors must have the same name as the class.
Constructors don't have a return type.
If you see code with a return type, it's a method with the same name as the class; it's not a constructor.
The constructor calls its superclass constructor, which calls its superclass
constructor, and so on all the way up to the Object constructor.
The Object constructor executes and then returns to the calling constructor, which runs to completion
and then returns to its calling constructor, and so on back down to the completion of the constructor of the
actual instance being created.
Constructors can use any access modifier.
Compiler will create a default constructor if you don't create any constructors in your class.
Default constructor is a no-arg constructor with a no-arg call to super().
First statement of every constructor must be a call either to this() OR an
overloaded constructor OR to super().
Compiler will add a call to super() unless you have already put in a call to this() or super().
Instance members are accessible only after the super constructor runs.
Abstract classes have constructors that are called when a concrete subclass is instantiated.
Interfaces do not have constructors.
If your superclass does not have a no-arg constructor, you must create a constructor
and insert a call to super() with arguments matching those of the superclass constructor.
Constructors are never inherited; thus they cannot be overridden.
Constructor can be directly invoked only by another constructor (using a call to super() or this()).
Calls to this() and super() cannot be in the same constructor. You can have one or the other, but never both.
Use static methods to implement behaviors that are not affected by the
state of any instances.
Use static variables to hold data that is class specific as opposed to instance
specific—there will be only one copy of a static variable.
All static members belong to the class, not to any instance.
A static method can't access an instance variable directly.
static methods can't be overridden, but they can be redefined.