Java SE: Programming Complete - 6
https://learn.oracle.com/ols/course/java-se-programming-complete/82508/85200
Inheritance
Extend Classes
java.lang.Objectis an ultimate paraent of any other class in Java- A class can have only one immediate parent, as multiple inheritance is not allow in Java
-
Objectdefines common, generic operations that all other Java classes inherit and reuse: - The
toStringmethod creates teext value for the object - The
equalsmethod compares a pair of objects - The
hashCodemethod generates int hash value for the object. - The
clonemethod produces a replica of the object. wait,notifyandnotifyAllmethods control threads.
Reuse Parent Class Code Through Inheritance
- The purpose of inheritance is to reuse generic superclass behaviors and state in subclass
Instantiating Classes and Accessing Objects
-
Heap memory allocated to store object (class instance) that contians
- Code of the specific subtype
-
All of the parents up the class hierarchy
-
Object reference can be of generic or specific type
public class Product {} public class Food extends Product {} Object x1 = new Food(); Product x2 = new Food(); Food x3 = new Food();In this example,
- x1 can only access code declared on the Object class
- x2 can access Object and Product
- x3 can access Object, Product and Food.
Rules of Reference Type Casting
- Casting is required to assign parent to child reference type.
- No casting is required to assign child to parent reference type
- Use
instanceofoperator before casting reference from generic to specific type.
Reference Code Within the Current or Parent Object
You can have same name variable in parent and subclass!!! Well-encapsulated code should only expose the method, so all variables should be private. In this case, no issues. BUT if the class have public/protected variable, then the same name variable in subclass will hide the parenet one. use
thisandsuperto distinguish them.
Define Subclass Contructors.
- The subclass contructor must invoke the superclass constructor.
- If superclass contains the no-arg constructor (default or explicitly defined), then the subclass can implicity invoke the superclass constructor.
- If superclass doesn’t provide the no-arg constructor, subclass constructor must explicitly invoke superclass constructor.
- Invocation of superclass constructor must be the first line of code in the subclass constructor.
Class and Object Initialization Summary
public class Shop {
static {}
public static void main(Stringp[] args) {
Product p1 = new Food();
}
}
public class Object {
static {}
public Object() {}
}
public class Product {
static {}
{}
public Production() {}
}
public class Food extends Product {
static {}
{}
public Food() {}
}
- Class loading and initialization execution order: (The following code is executed only once.)
Objectclass static initializer. (since Object is the root class)Shopclass static initializerProductclass static initializerFoodclass static iniializer- All code of the class must be loaded into memory first.
- It needs to be loaded only once per class
- Ojbect instantiation execution order:
Objectclass onstructorProductinstance initializerProductconstructorFoodinstance initializerFoodconstructor.- Each object instance must be initialized together with all of it’s parents.
- Each object instance memory contains object data and references to the rest of the class code (shared between all instances).
Override Methods and Use Polymorphism
- The subclass can override parent class methods.
- The subclass can widen but can’t narrow access of methods it overrides.
- Polymorphism in Java means when a method is declared in a superclass and is overridden in a subclass, the subclass method takes precedence without casting reference to specific subclass type.
Overrideannotation is optional, it’s used to ensure that subclass method signature matches the superclass method.
Define Abstract Classes and Methods
- Class can’t be directly instantiated if it is marked with the
abstractkeyword. (Even there is no abstract method inside) - The abstract class purpose is to be extended by one or more concrete subclasses.
- It may also contain abstract methods that describe method signatures, without a method body.
Define Final Classes and Methods
- The
finalkeyword can be used to limit class extensibility - Class can’t extend a class that is marked with the
finalkeyword - The subclass can’t override a superclass method that is marked with the
finalkeyword.
Override Object Class Operations
toString(),equals(),hashCode- The
equalscomare objects, the==operator compares values in the stack, so either primitive value, or two references. not real data inside the object. Java classes such asString,Number,LocalDateoverride theequalsmethod. - The
hashCodemethods should return same hash code ifequalsreturn true for two object. - The
hashCodeshould be immutable. - A
java.security.MessageDigestclass should be used to generate secure hash values.
Comments