Chapter 5 - Inheritence Flashcards

1
Q

What is Inheritence

A

is one of the fundamental concepts of object-oriented programming (OOP). It allows a class (subclass) to inherit properties and behaviors from another class (superclass). The subclass can reuse and extend the functionalities of the superclass, promoting code reuse and creating a hierarchical relationship between classes.

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

What is Extends keyword?

A

To establish inheritance in Java, you use the extends keyword to define the relationship between the subclass and superclass. The subclass inherits all the non-private members (fields and methods) of the superclass, including constructors, public and protected members, and package-private members.

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

Explain single and multilevel Inheritence

A

Single Inheritance: Java supports single inheritance, meaning a class can inherit from only one superclass. This ensures a clear and straightforward class hierarchy.

Multilevel Inheritance: Java supports multilevel inheritance, where a subclass can inherit from another subclass, forming a chain of inheritance.

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

Access modifiers during inheritence in java

A

Public: Members declared as public are accessible from any class, whether it is in the same package or a different one. When it comes to inheritance, a public member in a parent class is inherited by its subclasses.

Protected: Members declared as protected are accessible within the same package and also by subclasses, even if they are in a different package. This makes them useful for providing limited visibility to subclasses. Protected members can be inherited by subclasses.

Default (Package-private): If no access modifier is specified (i.e., no public, protected, or private keyword is used), the member is considered to have default visibility, which means it is accessible only within the same package. Default members can be inherited by subclasses within the same package.

Private: Members declared as private are accessible only within the same class. They are not visible to subclasses, even if the subclass is in the same package. Private members cannot be inherited by subclasses.

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

Method overriding during the inheritence

A

Method overriding is a concept in Java where a subclass provides a specific implementation for a method that is already defined in its superclass. Method overriding allows you to provide a new behavior for a method in the subclass while keeping the method signature (name, return type, and parameter list) the same as the one in the superclass.

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

Upcasting and downcasting of objects

A

Upcasting:

Upcasting is the process of casting an object reference to a more general (superclass) reference.

It is implicit and doesn’t require a cast operator. The Java compiler handles it automatically.

Upcasting is safe and always allowed because a subclass object is also an instance of its superclass.

Upcasting is useful for achieving polymorphism, where you can treat objects of different subclasses as instances of a common superclass.
Animal myDog = new Dog();

Downcasting:

Downcasting is the process of casting a more general (superclass) reference to a more specific (subclass) reference.

It requires an explicit cast operator and can potentially lead to a runtime ClassCastException if the object being cast is not an instance of the specified subclass.

You should only perform downcasting when you are certain that the object being referenced is an instance of the subclass you are trying to cast to.
Animal myAnimal = new Dog(); // Upcasting
Dog myDog = (Dog) myAnimal; // Downcasting to Dog (explicit cast)

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

Java Interface

A

interface is a type that defines a contract for classes. It specifies a set of abstract methods that a class implementing the interface must provide. Interfaces are used to achieve abstraction, multiple inheritance (in a limited form), and to define a common contract for a group of related classes.

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

How java interfaces support inheritence

A

interfaces support inheritance through a mechanism called interface inheritance. Interface inheritance allows a class to inherit multiple interfaces, effectively enabling a form of multiple inheritance for types.

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

Implementing Multiple Interfaces

A

A Java class can implement multiple interfaces by specifying a comma-separated list of interface names in the implements clause when declaring the class. This means that a single class can inherit multiple contracts (sets of abstract methods) from different interfaces.

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

Interface Hierarchies

A

Interfaces can also extend other interfaces, creating an interface hierarchy. When an interface extends another interface, the sub-interface inherits the abstract methods (and any default methods) of the parent interface. A class implementing the sub-interface must provide implementations for all inherited methods.

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

can interface implement multiple interface?

A

Yes, in Java, an interface can implement multiple other interfaces. This feature allows you to create complex hierarchies of interfaces and compose them to define the contract for a class. This is known as multiple interface inheritance.

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

Default methods in Java interfaces

A

feature to allow the addition of new methods to existing interfaces without breaking compatibility with classes that implement those interfaces.adding a new method to an existing interface would have required modifying all classes that implemented that interface, which could be impractical and lead to compatibility issues.

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

What is the “diamond problem” in Java, and how does Java address it?

A

The “diamond problem” is a complication that arises in languages that support multiple inheritance of classes. In Java, this problem is resolved by allowing a class to implement multiple interfaces but inherit from only one class. This avoids the ambiguity that can arise when multiple superclasses provide conflicting method implementations.

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

Explain the concept of method hiding in Java.

A

Method hiding occurs when a subclass defines a static method with the same name and signature as a static method in its superclass. This hides (but does not override) the superclass’s static method when called from the subclass. Method hiding is different from method overriding, which involves instance methods.

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

What is the “final” keyword used for in Java, and how does it relate to inheritance?

A

The final keyword can be applied to classes, methods, and fields. When applied to a class, it prevents the class from being extended (subclassed). When applied to a method, it prevents the method from being overridden in subclasses. When applied to a field, it makes the field a constant that cannot be modified.

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

Explain the “is-a” and “has-a” relationships in OOP. How do they relate to inheritance and composition?

A

The “is-a” relationship is typically associated with inheritance, where a subclass is considered a specialized version of a superclass. The “has-a” relationship is associated with composition, where a class contains an instance of another class as one of its fields. Understanding when to use inheritance and when to use composition is important in OOP design.

17
Q

What is the “Liskov Substitution Principle” (LSP) in OOP, and how does it relate to inheritance?

A

The Liskov Substitution Principle states that objects of a derived class should be able to replace objects of the base class without affecting the correctness of the program. It emphasizes that subclasses should adhere to the contract established by their superclass and maintain compatibility.

18
Q

What is the “superclass constructor chaining” in Java, and why is it important when working with inheritance?

A

Superclass constructor chaining involves calling constructors of the superclass in a subclass’s constructor using the super() keyword. It ensures that the initialization logic of the superclass is executed before initializing the subclass. Understanding constructor chaining is crucial to ensure proper object initialization in inheritance hierarchies.

19
Q

Discuss the concept of “method hiding” in Java, and how it differs from method overriding.

A

Method hiding occurs when a subclass defines a static method with the same name as a static method in its superclass. This hides the superclass’s static method when called from the subclass. It is different from method overriding, which involves instance methods and dynamic dispatch.

20
Q

Sealed classes and interfaces

A

A sealed class is a class that explicitly specifies which other classes or interfaces are allowed to extend it. By sealing a class, you limit the inheritance hierarchy to a predefined set of subclasses, making it clear which classes can be extended and preventing arbitrary subclassing. This enhances code predictability and security.

ex:
sealed class Shape permits Circle, Rectangle {}

21
Q

What is Aggregation?

A

Aggregation is an association represents a part of a whole relationship where a part can exist without a whole. It has a weaker relationship.

It is a specialized form of Association where all object has their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship.
Let’s take an example of the relationship between Department and Teacher. A Teacher may belong to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.

22
Q

What is Association?

A

Association is a relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.

Let’s take an example of the relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.

23
Q

What is Cohesion?

A

The term cohesion is used to indicate the degree to which a class has a single, well-focused responsibility.

Cohesion is a measure of how the methods of a class or a module are meaningfully and strongly related and how focused they are in providing a well-defined purpose to the system.

24
Q

What is Coupling?

A

Coupling refers to the degree to which one class knows about another class. If one class uses another class, that is coupling. Low dependencies between “artifacts” (classes, modules, components).There shouldn’t be too much of dependency between the modules, even if there is a dependency it should be via the interfaces and should be minimal.

25
Q

What is Delegation?

A

Hand over the responsibility for a particular task to another class or method.
It is a technique where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object.

26
Q
A