Inheritance Flashcards

(157 cards)

1
Q

What is the purpose of the this keyword in Java?

A

this refers to the current object instance whose method or constructor is being executed.

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

When is this used to resolve variable conflicts?

A

It is used when instance variables are shadowed by method parameters or local variables with the same name.

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

Example use of this for variable shadowing?

A

this.x = x assigns parameter x to instance variable x.

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

Can this be used to call another constructor?

A

Yes. this() calls another constructor in the same class.

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

When must this() be used in a constructor?

A

It must be the first statement in the constructor.

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

Can this be used to call methods?

A

Yes. this.methodName() explicitly calls a method of the current object.

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

Is using this to call methods required?

A

No. It is optional unless disambiguation is needed.

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

Can this refer to static members?

A

No. this refers only to instance context, not static context.

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

What happens if you use this inside a static method?

A

It causes a compile-time error because static methods have no instance.

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

Can this be returned from a method?

A

Yes. Returning this returns the current object reference.

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

What is constructor chaining using this?

A

It is the process of calling one constructor from another within the same class using this().

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

Difference between this and super?

A

this refers to current class instance, while super refers to the parent class instance.

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

Does this change during object lifetime?

A

No. It always refers to the current object instance for that method call.

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

Can this be passed as a parameter?

A

Yes. You can pass the current object reference to another method or constructor.

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

Is this available inside constructors?

A

Yes. It refers to the object being constructed.

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

What is inheritance in Java?

A

Inheritance is an OOP mechanism where one class acquires properties and methods of another class using extends.

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

What is the main purpose of inheritance?

A

It promotes code reuse and allows shared logic to be defined in a base (superclass) and reused by subclasses.

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

What is an IS-A relationship in inheritance?

A

It means a subclass is a specialized type of its superclass, e.g., Dog IS-A Animal.

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

What keyword is used to implement inheritance in Java?

A

extends.

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

What is a superclass?

A

A superclass is the parent class whose members are inherited by another class.

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

What is a subclass?

A

A subclass is a child class that inherits fields and methods from a superclass.

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

Does Java support multiple inheritance with classes?

A

No. Java supports single inheritance for classes but allows multiple inheritance through interfaces.

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

What is inherited from a superclass?

A

Accessible fields, methods, and behaviors are inherited, but constructors are not.

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

Can private members be inherited?

A

They are technically inherited but not directly accessible in subclasses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is method overriding in inheritance?
It occurs when a subclass provides a new implementation of a method already defined in the superclass.
26
Why is inheritance important for polymorphism?
Because runtime polymorphism relies on superclass references pointing to subclass objects.
27
What is the execution order when creating a subclass object?
Superclass constructor runs first, then subclass constructor.
28
What problem can excessive inheritance cause?
It can lead to tight coupling and rigid class hierarchies.
29
What is the alternative to inheritance for code reuse?
Composition, where classes contain other objects instead of extending them.
30
What is composition vs inheritance in one line?
Inheritance is IS-A relationship; composition is HAS-A relationship.
31
Which class is the superclass of every class in Java?
java.lang.Object.
32
What is the root class of Java’s class hierarchy?
Object is the root class from which all classes implicitly inherit.
33
Do all Java classes extend Object explicitly?
No. If a class does not explicitly extend another class, it automatically extends Object.
34
What are common methods inherited from Object class?
toString(), equals(), hashCode(), getClass(), notify(), notifyAll(), wait().
35
Why is Object class important?
It provides fundamental methods that every Java object inherits and can override.
36
Can a class avoid inheriting from Object?
No. All Java classes ultimately inherit from Object.
37
Which method of Object is commonly overridden for debugging?
toString().
38
Which Object method is used for logical comparison?
equals().
39
Which Object method is used for hashing collections?
hashCode().
40
What does getClass() return?
It returns the runtime class of the current object.
41
Is Object a final class?
No. It is not final because all classes must inherit from it.
42
Can Object methods be overridden?
Yes, many Object methods like equals(), hashCode(), and toString() are designed to be overridden.
43
Why must equals() and hashCode() be overridden together?
Because equal objects must produce the same hash code for collections like HashMap to work correctly.
44
What package does Object class belong to?
java.lang.
45
Why does Java not support multiple inheritance with classes?
Because it can create ambiguity when two parent classes define the same method, and the compiler cannot determine which implementation to use.
46
What is the diamond problem in multiple inheritance?
It occurs when a class inherits from two classes that both inherit from the same superclass, causing ambiguity about which inherited method to use.
47
Does Java support multiple inheritance at all?
Java does not support multiple inheritance of classes but supports multiple inheritance of type using interfaces.
48
Why are interfaces allowed for multiple inheritance?
Because interfaces traditionally contain only method declarations, so there is no implementation conflict.
49
How does Java handle method conflicts with multiple interfaces?
If two interfaces provide default methods with the same signature, the class must override and resolve the conflict explicitly.
50
What problem would arise if Java allowed multiple class inheritance?
Method ambiguity, complexity, and increased risk of errors in method resolution.
51
Example ambiguity scenario in multiple inheritance?
If two parent classes both define switchOn(), the child class would not know which version to inherit.
52
What is Java’s alternative to multiple inheritance for code reuse?
Interfaces and composition.
53
Which design principle is preferred over multiple inheritance?
Composition over inheritance.
54
What is composition in Java?
It is a design approach where a class contains references to other objects instead of extending multiple classes.
55
Why is composition safer than multiple inheritance?
It avoids ambiguity, reduces coupling, and provides more flexible design.
56
Does Java support multiple inheritance with interfaces and classes together?
Yes. A class can extend one class and implement multiple interfaces.
57
What keyword allows multiple inheritance of behavior contracts?
implements.
58
Is method ambiguity possible with interfaces?
Yes, but Java forces explicit override to resolve it, preventing ambiguity at runtime.
59
What is composition in OOP?
Composition is a design principle where one class contains an instance of another class, representing a HAS-A relationship.
60
What does HAS-A relationship mean?
It means one class owns or uses another class as part of its structure, e.g., Car HAS-A Engine.
61
How is composition implemented in Java?
By declaring an object of another class as a field inside a class.
62
Example of composition in Java?
class Car { Engine engine = new Engine(); }
63
What is the main advantage of composition?
It promotes code reuse and flexibility without inheritance coupling.
64
What is the difference between composition and inheritance?
Composition is HAS-A relationship, while inheritance is IS-A relationship.
65
Why is composition preferred over inheritance?
Because it reduces tight coupling and allows changing behavior at runtime.
66
Does composition support polymorphism?
Yes. The contained object can be referenced using interfaces or parent types.
67
What happens to composed objects when the container object is destroyed?
In strong composition, contained objects are typically destroyed with the parent.
68
What is strong vs weak composition?
Strong composition means contained object lifecycle depends on parent; weak composition (aggregation) allows independent lifecycle.
69
What is aggregation?
Aggregation is a weaker HAS-A relationship where contained objects can exist independently.
70
Real-world example of composition?
A House has Rooms; if the house is destroyed, the rooms are also gone.
71
Can composition replace inheritance?
Yes. Many designs use composition instead of inheritance for better flexibility.
72
Why do modern design principles favor composition?
It improves maintainability, reduces hierarchy complexity, and allows behavior changes dynamically.
73
What is the difference between aggregation and composition?
Aggregation is a weak HAS-A relationship where contained objects can exist independently, while composition is a strong HAS-A relationship where contained objects depend on the parent’s lifecycle.
74
What defines composition in OOP?
Composition is a strong ownership relationship where child objects are destroyed when the parent object is destroyed.
75
What defines aggregation in OOP?
Aggregation is a weak association where child objects can exist independently of the parent.
76
Example of composition?
A House has Rooms; if the house is destroyed, the rooms are also destroyed.
77
Example of aggregation?
A Library has Students; if the library closes, students still exist.
78
What is the key lifecycle difference between aggregation and composition?
In composition, child lifecycle depends on parent; in aggregation, it does not.
79
Which relationship is stronger: aggregation or composition?
Composition is stronger.
80
How is composition implemented in Java code?
The parent class usually creates and owns the child object internally.
81
How is aggregation implemented in Java code?
The parent class receives or references objects created externally.
82
Why is composition considered tighter coupling?
Because the child object’s existence is bound to the parent object.
83
Why is aggregation considered looser coupling?
Because child objects are independent and reusable elsewhere.
84
Can aggregation objects be shared?
Yes. The same object can be referenced by multiple parent objects.
85
Can composition objects be shared?
Usually no, because they belong exclusively to one parent.
86
UML symbol difference between aggregation and composition?
Aggregation uses a hollow diamond; composition uses a filled diamond.
87
Which design principle prefers aggregation over composition?
Low coupling and high cohesion principles encourage weaker relationships when possible.
88
Why does Java not support pointers like C or C++?
Because direct memory access via pointers can cause security, stability, and memory management issues, so Java uses references instead.
89
What does Java use instead of pointers?
Java uses object references that point to objects without exposing actual memory addresses.
90
What is the main advantage of references over pointers?
They prevent unsafe memory manipulation and improve program safety.
91
Can Java references access memory addresses directly?
No. Java does not allow direct memory address manipulation.
92
How does removing pointers improve security?
It prevents illegal memory access, buffer overflows, and memory corruption vulnerabilities.
93
How does lack of pointers help garbage collection?
JVM can freely move objects in memory and track references safely without programmer intervention.
94
Can programmers perform pointer arithmetic in Java?
No. Java does not allow pointer arithmetic.
95
Why does JVM need freedom to move objects in memory?
To optimize memory usage, defragment heap space, and improve garbage collection performance.
96
Do Java references behave exactly like C pointers?
No. References cannot be manipulated, incremented, decremented, or cast to raw addresses.
97
Is Java truly pointer-free internally?
No. JVM uses pointers internally, but they are hidden from developers.
98
What problem do pointers commonly cause in low-level languages?
Memory leaks, segmentation faults, dangling pointers, and undefined behavior.
99
Why is Java considered safer than pointer-based languages?
Because memory access is controlled by JVM rather than directly by the programmer.
100
Can you simulate pointer-like behavior in Java?
Only indirectly using references, arrays, or wrapper objects, but not real memory pointers.
101
Which design goal influenced Java’s decision to remove pointers?
Safety, portability, and reliability.
102
Why do we get NullPointerException if Java has no pointers?
Because Java uses object references, and when a reference is null and you try to access a method or field through it, the JVM throws NullPointerException.
103
What does null mean for a reference variable?
It means the reference does not point to any object in memory.
104
When does NullPointerException occur?
It occurs when code tries to use a null reference to call a method, access a field, or perform an operation requiring an object.
105
Is NullPointerException a compile-time or runtime error?
It is a runtime exception.
106
What operation typically triggers NullPointerException?
Using the dot operator on a null reference, e.g., obj.method().
107
Does Java internally use pointers?
Yes. JVM uses pointers internally, but developers only work with safe references.
108
Why does Java allow null references?
To represent absence of an object or uninitialized reference state.
109
How can you prevent NullPointerException?
By checking for null, using Objects.requireNonNull(), or applying Optional patterns.
110
Example of NullPointerException?
String s = null; s.length(); → throws exception.
111
Is null the same as an empty object?
No. null means no object exists, while an empty object is a valid instance.
112
Which exception class represents NullPointerException?
java.lang.NullPointerException.
113
Can primitives cause NullPointerException?
No. Only reference types can be null.
114
Why is NullPointerException considered common?
Because references may be unintentionally left uninitialized or set to null.
115
What is a safe comparison pattern to avoid NullPointerException?
"constant".equals(variable) instead of variable.equals("constant").
116
What is the purpose of the super keyword in Java?
super refers to the immediate parent class object and is used to access parent class methods, fields, and constructors.
117
When is super used to call a parent method?
It is used when a subclass wants to invoke a method defined in its superclass, especially if it is overridden.
118
Example of calling parent method using super?
super.display();
119
What does super() do in a constructor?
It calls the constructor of the immediate superclass.
120
Where must super() appear in a constructor?
It must be the first statement in the constructor.
121
What happens if you don’t call super() explicitly?
The compiler automatically inserts a call to the parent’s no-argument constructor.
122
What error occurs if parent has no default constructor and super() is missing?
A compile-time error occurs requiring explicit constructor call.
123
Can super access private members of parent class?
No. Private members are not accessible in subclasses.
124
Can super be used in static methods?
No. super requires an instance context.
125
Difference between this and super?
this refers to current class instance; super refers to parent class instance.
126
Can super access parent class fields?
Yes, even if they are hidden by subclass fields.
127
Why use super when a method is overridden?
To explicitly call the parent class implementation.
128
Can super refer to grandparent class directly?
No. super refers only to the immediate parent.
129
What is constructor chaining using super?
It is the process of calling a parent constructor from a child constructor.
130
Why must parent constructor run before child constructor?
Because the parent portion of the object must be initialized first.
131
Can we use both this() and super() in the same constructor?
No. Java does not allow both because only one constructor call is permitted as the first statement.
132
Why can’t this() and super() be used together in a constructor?
Because both must be the first statement, and a constructor can only have one first statement.
133
What does this() do in a constructor?
It calls another constructor in the same class.
134
What does super() do in a constructor?
It calls a constructor of the parent class.
135
Which must appear first in a constructor: this() or super()?
Either one can appear first, but only one is allowed and it must be the first statement.
136
What happens if neither this() nor super() is written?
The compiler automatically inserts super().
137
Can this() indirectly call super()?
Yes. If this() calls another constructor that uses super(), then super() is eventually invoked.
138
Why does Java enforce constructor call as first statement?
To ensure proper object initialization order from superclass to subclass.
139
What is constructor chaining?
It is the process of calling one constructor from another using this() or super().
140
Which constructor runs first during object creation?
The superclass constructor runs before subclass constructor.
141
What error occurs if this() or super() is not first statement?
Compile-time error: "Constructor call must be the first statement".
142
Can a constructor call multiple constructors directly?
No. It can call only one constructor directly.
143
What is object cloning in Java?
Object cloning is the process of creating a copy of an existing object with the same class type and field values.
144
Which method is used for cloning objects in Java?
The clone() method of java.lang.Object.
145
What does clone() return?
It returns an Object reference that must be cast to the appropriate type.
146
Is cloning similar to a copy constructor?
Yes. Both create new objects with the same state as an existing object.
147
What interface must a class implement to allow cloning?
Cloneable interface.
148
What happens if clone() is called on a class that does not implement Cloneable?
A CloneNotSupportedException is thrown.
149
Is Object.clone() shallow or deep copy by default?
Shallow copy.
150
What is a shallow copy?
It copies primitive values and object references, not the referenced objects themselves.
151
What is a deep copy?
It copies primitive values and also duplicates all referenced objects recursively.
152
Why is shallow cloning risky?
Because both original and clone share references to the same internal objects.
153
How can you implement deep cloning?
By manually cloning referenced objects inside clone() or using serialization-based copying.
154
Why is cloning considered tricky in Java?
Because it requires handling Cloneable, exceptions, casting, and deep vs shallow copy logic.
155
Can clone() be overridden?
Yes. Classes often override clone() to control cloning behavior.
156
What access modifier does Object.clone() have?
protected.
157
Why do many developers avoid cloning in modern Java?
Because copy constructors or factory methods are safer and clearer alternatives.