CH6 - Class Design Flashcards
Proper Class Design
Code Reusability
Increased functionality
Standardization
What is Object
An object in Java is an instance of a class that contains state (fields/variables) and behavior (methods). Object is in memory
When to use ‘protected’?
-When you want to allow access to subclasses but prevent access to unrelated classes.
-When defining frameworks or APIs where only extending classes should use certain methods.
Best use case: When you want to allow inheritance but keep some level of encapsulation.
Deafult Constructor
Compiler only inserts the default constructor when no constructors are defined.
this vs this()
this refers to an instance of the class
this() refers to a constructor call within the class
Calling this()
The this() call must be the first statement in the constructor. There can be only one call to this() in any constructor
Constructor Rules
✅ Class can contain many overloaded constructors, provided signature for each is distinct
✅ The compiler inserts a default no-argument constructor if no constructors are declared
✅ If a constructor calls this(), then it must be the first line of the constructor
✅ Java doesn’t allow cyclic constrtuctor calls
super() in child class constructor
Java compiler automatically inserts a call to the no-argument constructor super() if you dont explicitly call this() or super() as the first line of constructor.
public class Money {
public Money() {
super();
}
}
Default constructor tip
✅ If a constructor calls super() or this(), then it must be the first line of the constructor
✅ If the constructor does not contain a this() or super() reference, then the compiler automatically inserts super() with no arguments as the first line of the constructor
Order of class initialization
- Initialize the superclass of X
- Process all static variable declarations in the order in which they appear in the class
- Process all static initializers in the order in which they appear in the class
Initialize Instance of X class
- Init Class X if it has not been previously init.ed.
- Init the superclass instance of X
- Process all instance variable declarations in the order in which they appear in the class
- Process all instance initializers in the order in which they appear in the class
- Initialize the constructor, including any overloaded constructors referenced with this()
Method override rules
- The method in the child class must have the same signature as the method in the parent class
- The method in the child class must be at least as accesible as the method in the parent class.
- The method in the child class may not declare a checked exception that is new or broader than the class of any exception declared in the parent class method.
- If the method returns a value, it must be the same or a subtype of the method in the parent class, known as covarint return types.
Covariant Types
Java type covariance refers to the ability of a method in a subclass to return a more specific type than the method it overrides in the superclass. This feature was introduced in Java 5 to improve type safety and reduce the need for type casting.
Cautions for method overriding
- Pay attention to access modifiers
- method signature
Hiding static methods
A hidden method occurs when a child class defines a static method with the same name and signature as an inderited static method defined in parent class.
Static Method Inheritance
✅ Static methods belong to the class, not to individual objects.
✅ A subclass inherits static methods from its parent class, but it cannot override them.
✅ Instead of overriding, a static method in a subclass hides the superclass method.
Important Rules About Static Method Inheritance
✅ Static methods are inherited but do not support dynamic method dispatch (runtime polymorphism).
✅ Method hiding occurs instead of method overriding.
✅ Method calls depend on the reference type, not the object type.
❌ super.display(); does not work with static methods in subclasses.
Hidden variable
✅ A hidden variable occurs when a child class defines a variable with the same name as an inherited variable defined in the parent class
Final method
By marking a method final, you forbid a child class from replacing this method.
This rule is in place both when you override a methoda and when you hide a method.
final static method can’t be hidden in child class
Abstract class rules
📌 Only instance methods can be marked abstract within a class, not variables, constructors or static methods.
📌 An abstract class include zero or more abstract methods, while a non-abstract class cannot contain any.
📌 A non-abstract class that extends abstract class must implement all inherited abstract methods.
📌 Overriding an abstract method follows the existing ruels for overriding methods
abstract modifier place
Like final modifier the abstract modifier can be placed before or after the access modifier in class and method declarations.
Constructor on abstract vs concrete class
Constructor in an abstract class can be called only when it is being initialized by non-abstract class as abstract classes cannot be instantiated.
abstract vs private modifiers
While it is not possible to declare a method abstract and private it is possible (albeit redundant) to declare a method final and private
Immutable class
📌 Mark the class as final or make all of the constructors private
📌 Mark all the instance variables private and final
📌 Dont define any setter methods
📌 Dont allow referenced mutable objects to be modified
📌 User a constructor to set all properties of the object, making a copy if needed
Covariant Return Type
Covariant return types allow the child method to return a subtype of the parent’s return type.