Sei sulla pagina 1di 6

o Can we overload main method ?

Yes, main method can be overloaded. Overloaded main method has to be called
from inside the "public static voidmain(String args[])" as this is the entry point when
the class is launched by the JVM.

o Constructor returns a value but, what ?

So when you call the constructor using a new keyword you get an object. Though it
doesnt explicitly return something but instead it creates or constructs something
which you can use as an instance of a class. yes, it is the current class instance. (We
cannot use return type yet it returns a value)

o Can we create a program without main method ?

Yes You can compile and execute without main method By using static block. But
after static block executed (printed) you will get an error saying no main
methodfound. And Latest INFO --> YOU cant Do this with JAVA 7 version. IT will not
execute.

o What are the 6 ways to use this keyword ?

1. this can be used to refer current class instance variable.

2. this can be used to invoke current class method (implicitly)

3. this() can be used to invoke current class constructor.

4. this can be passed as an argument in the method call.

5. this can be passed as argument in the constructor call.

6. this can be used to return the current class instance from the method.

o Why multiple inheritance is not supported in java ?

In C++ there is a possibility to get into this trap though it provides alternates to solve
this. In java this can never occur as there is no multiple inheritance. Here even if two
interfaces are going to have same method, the implementing class will have only one
method and that too will be done by the implementer.

o Why use aggregation ?


Aggregation is a special form of association. It is a relationship between two classes like
association, however its a directional association, which means it is strictly a one way
association. It represents a HAS-A relationship.

o Can we override the static method ?

No,Static methods can't be overriden as it is part of a class rather than an object. But
one can overload static method.

o What is covariant return type ?

Covariant return, means that when one overrides a method, the return type of the
overriding method is allowed to be a subtype of the overridden method's return type.
To clarify this with an example, a common case is Object.clone() - which is declared
to return a type of Object .

o What are the three usage of super keyword?

It is used inside a sub-class method definition to call a method defined in


the superclass. Private methods of the super-class cannot be called. Only public and
protected methods can be called by the super keyword. It is also used by class
constructors to invoke constructors of its parent class.

o Why use instance initializer block?

In a Java program, operations can be performed on methods, constructors


and initialization blocks. Instance Initialization Blocks or IIB are used to
initialize instance variables. IIBs are executed before constructors. They run each
time when object of the class is created.
o What is the usage of blank final variable ?

A blank final is a final variable whose declaration lacks an initializer. You then must
assign that blank final variable in a constructor. The final property of class must have
a value assigned before object is created. ... This is how you make immutable objects
and it is used in the builder pattern.

o What is marker or tagged interface ?

It is used to convey to the JVM that the class implementing an interface of this
category will have some special behavior. Hence, an empty interface in java is called
a marker interface. Marker interface is also called tag interface by some java
gurus.

o What is runtime polymorphism or dynamic method dispatch ?


Dynamic method dispatch is a mechanism by which a call to an
overridden method is resolved atruntime. This is how java implements runtime
polymorphism. When an overridden method is called by a reference, java
determines which version of that method to execute based on the type of object it refer
to.

o What is the difference between static and dynamic binding ?

Static binding happens at compile-time while dynamic binding happens at runtime.


... Therefore that is runtime or dynamic binding. Your invocation is bound to the
Animal class at compile time because you've specified the type
o How downcasting is possible in java ?

upcasting means casting the object to a supertype, while downcasting means casting
to a subtype. Injava, upcasting is not necessary as it's done automatically. And it's
usually referred as implicit casting. ... Actually at runtime it could be different
andjava will throw a ClassCastException , would that happen.
o What is the purpose of private constructor?

A singleton is a design pattern that allows only one instance of your class to be
created, and this can be accomplished by using a private constructor. Yes and it is
used to prevent instantiation and subsequently overriding. This is most often used
in singleton classes.

o What is object cloning ?

The object cloning is a way to create exact copy of an object. For this
purpose,clone() method of Object class is used to clone an object. The
java.lang.Cloneable interface must be implemented by the class whose object
clone we want to create.

Potrebbero piacerti anche