Core Java Interview Question Page 30
Objects and Classes
Question: What's the difference between constructors and other methods
Answer:
Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.
Question: What is the difference between Overloading and Overriding
Answer: Overloading : Reusing the same method name with different arguments and perhaps a different return type is called as overloading
Overriding : Using the same method name with identical arguments and return type is know as overriding
Question: What do you understand by late binding or virtual method Invocation. (Example of runtime polymorphism)
Answer:
When a compiler for a non object oriented language comes across a method invocation, it determines exactly what target code should be called and build machine language to represent that call. In an object oriented language, this is not possible since the proper code to invoke is determined based upon the class if the object being used to make the call, not the type of the variable. Instead code is generated that will allow the decision to be made at run time. This delayed decision making is called as late binding
Question: Can overriding methods have different return types
Answer:
No they cannot have different return types
Question: If the method to be overridden has access type protected, can subclass have the access type as private
Answer:
No, it must have access type as protected or public, since an overriding method must not be less accessible than the method it overrides
Question: Can constructors be overloaded
Answer:
Yes constructors can be overloaded
Question: What happens when a constructor of the subclass is called
Answer:
A constructor delays running its body until the parent parts of the class have been initialized. This commonly happens because of an implicit call to super() added by the compiler. You can provide your own call to super(arguments..) to control the way the parent parts are initialized. If you do this, it must be the first statement of the constructor.
Question: If you use super() or this() in a constructor where should it appear in the constructor
Answer:
It should always be the first statement in the constructor
Question: What is an inner class
Answer:
An inner class is same as any other class, but is declared inside some other class
Question: How will you reference the inner class
Answer:
To reference it you will have to use OuterClass$InnerClass
Question: Can objects that are instances of inner class access the members of the outer class
Answer:
Yes they can access the members of the outer class
Question: What modifiers may be used with an inner class that is a member of an outer class?
Answer:
A (non-local) inner class may be declared as public, protected, private, static, final, or abstract
Question: Can inner classes be static
Answer:
Yes inner classes can be static, but they cannot access the non static data of the outer classes, though they can access the static data
Question: Can an inner class be defined inside a method
Answer:
Yes it can be defined inside a method and it can access data of the enclosing methods or a formal parameter if it is final
Question: What is an anonymous class
Answer:
Some classes defined inside a method do not need a name, such classes are called anonymous classes
Question: What are the rules of anonymous class
Answer:
The class is instantiated and declared in the same place The declaration and instantiation takes the form
new Xxxx () {// body}
Where Xxxx is an interface name. An anonymous class cannot have a constructor. Since you do not specify a name for the class, you cannot use that name to specify a constructor