0% found this document useful (0 votes)
80 views4 pages

Owen R. Largo Bit - CT 2B Day 03/05/2021

Inheritance allows classes to inherit behavior and data from other classes. Java supports single inheritance, multi-level inheritance, and limited multiple inheritance through interfaces. Multiple inheritance is not fully supported in Java because it can introduce complexity and confusion. Inheritance is used for code reuse and enabling polymorphism through class hierarchies, while composition is more flexible. The key differences between inheritance and composition are that inheritance creates an "is-a" relationship and composition creates a "has-a" relationship.

Uploaded by

Owen Largo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views4 pages

Owen R. Largo Bit - CT 2B Day 03/05/2021

Inheritance allows classes to inherit behavior and data from other classes. Java supports single inheritance, multi-level inheritance, and limited multiple inheritance through interfaces. Multiple inheritance is not fully supported in Java because it can introduce complexity and confusion. Inheritance is used for code reuse and enabling polymorphism through class hierarchies, while composition is more flexible. The key differences between inheritance and composition are that inheritance creates an "is-a" relationship and composition creates a "has-a" relationship.

Uploaded by

Owen Largo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

OWEN R.

LARGO BIT – CT 2B DAY 03/05/2021

1: What is Inheritance in Java?


Answer: Inheritance is an Object oriented feature which allows a class to inherit behavior and
data from other class. For example, a class Car can extend basic feature of Vehicle class by using
Inheritance. One of the most intuitive examples of Inheritance in the real world is Father-Son
relationship, where Son inherit Father's property. If you don't know, Inheritance is the quick
way to become rich :)

2: What are different types of Inheritance supported by Java? 


Answer: Java supports single Inheritance, multi-level inheritance and at some extent multiple
inheritances because Java allows a class to only extend another class, but an interface in Java
can extend multiple inheritances.

3: Why multiple Inheritance is not supported by Java?


Answer: Java is introduced after C++ and Java designer didn't want to take some C++ feature
which is confusing and not essential. They think multiple inheritances is one of them which
doesn't justify complexity and confusion it introduces. You can also check why multiple
inheritances are not supported in Java for more reasons and discussion around this.

4: Why Inheritance is used by Java Programmers?


Answer: Inheritance is used for code reuse and leveraging Polymorphism by creating a type
hierarchy. It's better to use Inheritance for type declaration but for code reuse composition is a
better option because it's more flexible. See this article for learning more about why
Composition is better than Inheritance.
OWEN R. LARGO BIT – CT 2B DAY 03/05/2021

6: What is the syntax of Inheritance? 


Answer: You can use either extends of implements keyword to implement Inheritance in Java.
A class extends another class using extends keyword, an interface can extend another interface
using extend keyword, and a class can implement an interface using implements keyword in
Java.

7: What is the difference between Inheritance and Encapsulation? 


Answer: Inheritance is an object oriented concept which creates a parent-child relationship. It is
one of the ways to reuse the code written for parent class but it also forms the basis of
Polymorphism. On the other hand, Encapsulation is an object oriented concept which is used to
hide the internal details of a class e.g. HashMap encapsulate how to store elements and how to
calculate hash values.

8: What is the difference between Inheritance and Abstraction? 


Answer: Abstraction is an object oriented concept which is used to simply things by abstracting
details. It helps in the designing system. On the other hand, Inheritance allows code reuse. You
can reuse the functionality you have already coded by using Inheritance.

9: What is the difference between Polymorphism and Inheritance?


Answer: Both Polymorphism and Inheritance goes hand on hand, they help each other to
achieve their goal. Polymorphism allows flexibility, you can choose which code to run at
runtime by overriding.  See the for more details.

10: What is the difference between Composition and Inheritance in OOP?


Answer: One of the good to check the candidate's object-oriented programming skills. There
are several differences between Composition and Inheritance in Java, some of them are
following:

1. The Composition is more flexible because you can change the implementation at runtime by
calling setXXX() method, but Inheritance cannot be changed i.e. you cannot ask a class to
implement another class at runtime.

2. Composition builds HAS-A relationship while Inheritance builds IS-A relationship e.g. A Room
HAS A Fan, but Mango IS-A Fruit.

3. The parent-child relationship is best represented using Inheritance but If you just want to use
the services of another class use Composition.
OWEN R. LARGO BIT – CT 2B DAY 03/05/2021

11. Can we override static method in Java?


Answer: No, you cannot override a static method in Java because it's resolved at compile time.
In order for overriding to work, a method should be virtual and resolved at runtime because
objects are only available at runtime. This is one of the tricky Java s, where interviewer tries to
confuse you. A programmer is never sure about whether they can override or overload a static
method in Java.
12. Can we overload a static method in Java?
Answer: Yes, you can overload a static method in Java. Overloading has nothing to do with
runtime but the signature of each method must be different. In Java, to change the method
signature, you must change either number of arguments, type of arguments or order of
arguments.

13. Can we override a private method in Java?


Answer: No, you cannot override a private method in Java because the private method is not
inherited by the subclass in Java, which is essential for overriding. In fact, a private method is
not visible to anyone outside the class and, more importantly, a call to the private method is
resolved at compile time by using Type information as opposed to runtime by using the actual
object.

14: What is method hiding in Java?


Answer: Since the static method cannot be overridden in Java, but if you declare the same
static method in subclass then that would hide the method from the superclass. It means, if you
OWEN R. LARGO BIT – CT 2B DAY 03/05/2021

call that method from subclass then the one in the subclass will be invoked but if you call the
same method from superclass then the one in superclass will be invoked. This is known as
method hiding in Java.

15: Can a class implement more than one interface in Java?


Answer: Yes, a class can implement more than one interface in Java e.g. A class can be both
Comparable and Serializable at the same time. This is why the interface should be the best use
for defining Type as described in Effective Java. This feature allows one class to play a
polymorphic role in the program.

You might also like