CH6 - Class Design Flashcards

1
Q

Proper Class Design

A

Code Reusability
Increased functionality
Standardization

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Object

A

An object in Java is an instance of a class that contains state (fields/variables) and behavior (methods). Object is in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When to use ‘protected’?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Deafult Constructor

A

Compiler only inserts the default constructor when no constructors are defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

this vs this()

A

this refers to an instance of the class
this() refers to a constructor call within the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Calling this()

A

The this() call must be the first statement in the constructor. There can be only one call to this() in any constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Constructor Rules

A

✅ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

super() in child class constructor

A

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();
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Default constructor tip

A

✅ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Order of class initialization

A
  1. Initialize the superclass of X
  2. Process all static variable declarations in the order in which they appear in the class
  3. Process all static initializers in the order in which they appear in the class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Initialize Instance of X class

A
  1. Init Class X if it has not been previously init.ed.
  2. Init the superclass instance of X
  3. Process all instance variable declarations in the order in which they appear in the class
  4. Process all instance initializers in the order in which they appear in the class
  5. Initialize the constructor, including any overloaded constructors referenced with this()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method override rules

A
  1. The method in the child class must have the same signature as the method in the parent class
  2. The method in the child class must be at least as accesible as the method in the parent class.
  3. 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.
  4. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Covariant Types

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Cautions for method overriding

A
  1. Pay attention to access modifiers
  2. method signature
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Hiding static methods

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Static Method Inheritance

A

✅ 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.

17
Q

Important Rules About Static Method Inheritance

A

✅ 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.

18
Q

Hidden variable

A

✅ A hidden variable occurs when a child class defines a variable with the same name as an inherited variable defined in the parent class

19
Q

Final method

A

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

20
Q

Abstract class rules

A

📌 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

21
Q

abstract modifier place

A

Like final modifier the abstract modifier can be placed before or after the access modifier in class and method declarations.

22
Q

Constructor on abstract vs concrete class

A

Constructor in an abstract class can be called only when it is being initialized by non-abstract class as abstract classes cannot be instantiated.

23
Q

abstract vs private modifiers

A

While it is not possible to declare a method abstract and private it is possible (albeit redundant) to declare a method final and private

24
Q

Immutable class

A

📌 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

25
Q

Covariant Return Type

A

Covariant return types allow the child method to return a subtype of the parent’s return type.