top of page

Java Questions & Answers ( II )

  • Writer: Mahesh Bhat M
    Mahesh Bhat M
  • Jun 25, 2017
  • 3 min read

1) Which class is the superclass for every class.

Object class.

2) What is composition?

Holding the reference of the other class within some other class is known as composition.

3) What is difference between aggregation and composition?

Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).

4) Why Java does not support pointers?

Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

5) What is method overriding:

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

6) Can we override static method?

No, you can't override the static method because they are the part of class not object.

7) Why we cannot override static method?

It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

8) Can we override the overloaded method?

Yes.

9) What is final variable?

If you make any variable as final, you cannot change the value of final variable(It will be constant).

10) What is final method?

Final methods can't be overriden.

11) What is final class?

Final class can't be inherited.

12) What is blank final variable?

A final variable, not initalized at the time of declaration, is known as blank final variable.

13) Can we intialize blank final variable?

Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.

14) Can you declare the main method as final?

Yes, such as, public static final void main(String[] args){}.

15) What is Runtime Polymorphism?

Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

16) Can you achieve Runtime Polymorphism by data members?

No.

17) What is the difference between static binding and dynamic binding?

In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.

18) Can there be any abstract method without abstract class?

No, if there is any abstract method in a class, that class must be abstract.

19) Can you use abstract and final both with a method?

No, because abstract method needs to be overridden whereas you can't override final method.

20) Is it possible to instantiate the abstract class?

No, abstract class can never be instantiated.

21) What is interface?

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.

22) Can you declare an interface method static?

No, because methods of an interface is abstract by default, and static and abstract keywords can't be used together.

23) Can an Interface be final?

No, because its implementation is provided by another class.

24) What is marker interface?

An interface that have no data member and method is known as a marker interface.For example Serializable, Cloneable etc.

2) When can an object reference be cast to an interface reference?

An object reference can be cast to an interface reference when the object implements the referenced interface.

Comments


Recent Posts

© 2023 by Kathy Schulders. Proudly created with Wix.com 

  • Grey Twitter Icon
bottom of page