Objects and Classes
|
|
What's the difference between
constructors and other methods |
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.
|
|
What is the difference between
Overloading and Overriding |
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
|
|
What do you understand by late
binding or virtual method Invocation. (Example of runtime
polymorphism) |
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
|
|
Can overriding methods have
different return types |
No they cannot have different return types
|
|
If the method to be overridden
has access type protected, can subclass have the access type as
private |
No, it must have access type as protected or public, since an
overriding method must not be less accessible than the method it
overrides
|
|
Can constructors be overloaded |
Yes constructors can be overloaded
|
|
What happens when a constructor
of the subclass is called |
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.
|
|
If you use super() or this() in
a constructor where should it appear in the constructor |
It should always be the first statement in the constructor
|
|
What is an inner class |
An inner class is same as any other class, but is declared
inside some other class
|
|
How will you reference the inner
class |
To reference it you will have to use OuterClass$InnerClass
|
|
Can objects that are instances
of inner class access the members of the outer class |
Yes they can access the members of the outer class
|
|
What modifiers may be used with
an inner class that is a member of an outer class? |
A (non-local) inner class may be declared as public, protected,
private, static, final, or abstract
|
|
Can inner classes be static |
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
|
|
Can an inner class be defined
inside a method |
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
|
|
What is an anonymous class |
Some classes defined inside a method do not need a name, such
classes are called anonymous classes
|
|
What are the rules of anonymous
class |
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
|