Chapter 5 - Inheritence Flashcards
What is Inheritence
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.
What is Extends keyword?
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.
Explain single and multilevel Inheritence
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.
Access modifiers during inheritence in java
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.
Method overriding during the inheritence
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.
Upcasting and downcasting of objects
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)
Java Interface
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 java interfaces support inheritence
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.
Implementing Multiple Interfaces
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.
Interface Hierarchies
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.
can interface implement multiple interface?
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.
Default methods in Java interfaces
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.
What is the “diamond problem” in Java, and how does Java address it?
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.
Explain the concept of method hiding in Java.
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.
What is the “final” keyword used for in Java, and how does it relate to inheritance?
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.