Java interview questions with answers
Home Java Interview Questions Revision Notes Resources Interview Tips Interview Samples

Valid HTML 4.01 Transitional

Java Interview Questions

Part 4 - Constructors, Inheritance and Polymorphism

Explain Inheritance?
Inheritance is a concept where the properties and behaviour of a class is reused in another class. The class which is being reused or inherited is called the super class or the parent class. The class which is inheriting is called the subclass or the child class of the inherited class.

Does Java support multiple inheritance?
No. Java Supports only single inheritance.

Why Java doesn’t support muliple inheritance?
When a class inherits from more than class, it will lead to inheritance path amibiguity (this is normally calledthe diamond problem). Say A is the super class of B and C & D is a subclass of both B and C. D inherits properties of A from two different inheritance paths ie via both B & C. This leads to ambiguity and related problems, so multiple inheritance is not allowed in Java.

Which keyword is used for inheriting from another class?
extends keyword is used  for inheritance.

Can a subclass be referenced for a super class object?
No. If Vehicle is super class and Car is the subclass then the following reference would be wrong – Car c = new Vehicle();

Can a parent class be referenced for a subclass object?
Yes. The below is an example  :
Vehicle v = new Car();

Can an interface be referenced for any object?
Yes. We can create a object reference for an interface. The object should have provided the implementation for the object.
Runnable r = new Test();
Test class should have implemented the runnable interface and overridded the run method.

What are constructors?
Constructors are used to initialise an object. Constructors are invoked when the new operator on the class are called. 

What are the decalaration of a constructor?
Constructor name should be the same as class name.
Constructors doesn’t return anything – not even void.

Does constructors throw exceptions?
Yes. Like methods constructors can also throw exceptions.

Can constructors be overloaded?
Yes. Constructors can be overloaded.

Is it compulsory to define a constructor for a class?
No. If you don’t define a constructor, the compiler will provide a default constructor.

What is a default constructor?
Default constructor is a no argument constructor which initialises the instance variables to their default values. This will be provided by the compiler at the time of compilation. Default constructor will be provided only when you don’t have any constructor defined.

Explain about “this” operator?
“this” is used to refer the currently executing object and it’s state. “this” is also used for chaining constructors and methods. 

Explain about “super” operator?
“super” is used to refer to the parent object of the currently running object. “super” is also to invoke super class methods and classes.

What is the sequence of constructor invocation?

Can a constructor be defined for an interface?

Explain Polymorphism?

What is Overloading?

Explain Overriding?

What is the difference between Overloading and Overriding?

When overriding a static method, can it be changed to non-static method?

Can the overriding method change the exceptions in the throws clause of the method?



 



Pls send your questions, suggestions and feedback to j2eeguide@yahoo.com

© Copyright JGuide 2006, All Rights Reserved.