Skip to main content

Inheritance Interview Questions in Java



Most Frequently asked Interview Questions and Answer in Java:


           In this Java Interview Question blog, we have covered almost total 100+ important Inheritance in OOPs concepts  interview questions and answer in java for freshers and experienced candidates one by one topics.

Inheritance Interview Questions in Java



            This post on JAVA Interview Questions and Answers is prepared to help you understand the basic concepts of Java programming for top composes interview purposes. All the most important JAVA concepts are explained here with examples for your easy understanding.

             This blog covers core JAVA topics, advance java topics and also industry level project related most important Interview Questions with examples to make you get ready perfectly to face any JAVA interview confidently.
Keywords Interview Questions and Answer --------- Static keyword and Final Keyword


Most Popular Java Interview Questions:

          Given below is a comprehensive list of most important and commonly asked about Inheritance  in OOPs Concept basic and advanced Java programming interview questions with detailed answers.





1.What is the purpose of ‘this’ keyword in java?

           In Java, ‘this’ keyword refers to current instance of the object. It is useful for differentiating between instance and local variables. It can be used to call constructors. Or it can be used to refer to the instance.
In case of method overriding, this is used for falling the method of current class.


2. Explain the concept of Inheritance?

            Inheritance is an important concept in OOPs. Some objects share certain characteristics and behavior. By using Inheritance, we can put the common behavior and characteristics in a base class which is also known as super class. Then all the objects with common behavior inherit from the same base class. It is also represented by IS-A relationship.
Inheritance promotes, code reuse, method overriding and polymorphism.


3. Which class in Java is super class of every other class?


          Java is an object oriented programming language. In Java, Object class is the superclass of every other class.


4. Why Java does not support multiple inheritance?

             Multiple Inheritance means that a class can inherit behavior from two or more than two parent classes. The issue with Multiple Inheritance is that both parent classes may have different implementation for the same method. So they have different ways of doing the same thing.  Now which implementation should the child class choose? This leads to ambiguity in Multiple Inheritance. This is the main reason to not supporting Multiple Inheritance implementation in Java
             Lets say you have a class TV and another class Atom Bomb. Both have method switchOn() but only TV has switchOff() method. If your class inherits from both these parent classes then you have an issue that you can switchOn() both parents, but switchOff will only switchOff() TV. But you can implement multiple interfaces in Java.


5. In OOPS, what is meant by composition?

              Composition is also known as “has-a” relationship. In composition, “has-a” relation relates two classes. E.g. Class Car has a steering wheel. If a class holds the instance of another class, then it is called composition.


6. How aggregation and composition are different concepts?

              In OOPS, Aggregation and Composition are the type of association relations. A composition is a strong relationship. If the composite object is destroyed, then all its parts are also destroyed. E.g. A Car has a Steering Wheel. If Car object is destroyed, then there is no meaning of the Steering Wheel. In Aggregation, the relationship is weaker than Composition. E.g. A Library has students. If a Library is destroyed, Students still exist. So Library and Student are related by Aggregation. A Library has Books. If Library is destroyed, the Books are also destroyed. Books of a Library cannot exist without the Library. So Book and Library are related by Composition.


7. Why there are no pointers in Java?

                 In Java there are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory location. JVM is free to move the objects within VM memory. The absence of pointers help to managing memory and garbage collection effectively in java. Also it provides developers with convenience of not getting worried about memory allocation and deallocation.


8. If there are no pointers in Java, then why do we get exception NullPointerException? 

             In Java, the pointer equivalent is Object reference. When we use a .it points to object reference. So JVM uses pointers but programmer only see object references.In case an object reference points to null object, and we try to access a method or member variable on it, then we get exception NullPointerException.


9. What is the purpose of ‘super’ keyword in java?

                ‘super’ keyword is used in the methods or constructor of a child class. It refers to immediate parent class of an object. By using ‘super’ we can call a method of parent class from the method of a child class. We can also call the constructor of a parent class from the constructor of a child class by using ‘super’ keyword.


10. Is it possible to use super() and this() both in same constructor?

               No, Java does not allow using both this() and super() in same constructor. As per Java specification, this() and super() must be the first statement in a constructor.


11.What is the meaning of object cloning in Java?

             Object.clone() method is used for creating an exact copy of object in Java. It acts like a copy constructor. It creates and returns copy of the object, with the same class and with all the fields having same values as of the original object. One disadvantage of cloning is that the return type is only Object. It has to be explicitly cast to actual type.






Comments

Popular posts from this blog

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin

Cloud Computing Interview Questions

Cloud Computing 1. What are the benefits of Cloud Computing? There are ten main benefits of Cloud Computing: Flexibility : The businesses that have fluctuating bandwidth demands need the flexibility of Cloud Computing. If you need high bandwidth, you can scale up your cloud capacity. When you do not need high bandwidth, you can just scale down. There is no need to be tied into an inflexible fixed capacity infrastructure. Disaster Recovery : Cloud Computing provides robust backup and recovery solutions that are hosted in cloud. Due to this there is no need to spend extra resources on homegrown disaster recovery. It also saves time in setting up disaster recovery. Automatic Software Updates : Most of the Cloud providers give automatic software updates. This reduces the extra task of installing new software version and always catching up with the latest software installs. Low Capital Expenditure : In Cloud computing the model is Pay as you Go. This means there is very

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful hardware with multipl