Q) Can I have multiple main methods in
the same class?
A) No the program fails to compile. The
compiler says that the main method is already defined in the class.
Q) Constructor
The
automatic initialization is performed through the constructor, constructor has
same name has class name. Constructor has no return type not even void. We can
pass the parameters to the constructor. this() is used to invoke a constructor
of the same class. Super () is used to invoke a super class constructor.
Constructor is called immediately after the object is created before the new
operator completes.
à Constructor
can use the access modifiers public, protected, private or have
no access modifier
à
Constructor can not use the modifiers abstract, static, final,
native, synchronized or strictfp
à Constructor
can be overloaded, we cannot override.
à
You cannot use this() and Super() in the
same constructor.
Class A(
A(){
System.out.println(“hello”);
}}
Class B extends A {
B(){
System.out.println(“friend”);
}}
Class print {
Public static void main (String args []){
B b = new B();
}
o/p:- hello friend
Q) Diff Constructor & Method
Constructor
|
Method
|
Use to instance of a class
|
Grouping java statement
|
No return type
|
Void (or) valid return type
|
Same name as class name
|
As a name except the class method name, begin with
lower case.
|
“This” refer to another
constructor in the same class
|
Refers to instance of class
|
“Super” to invoke the super
class constructor
|
Execute an overridden method in the super class
|
“Inheritance” cannot be inherited
|
Can be inherited
|
We can “overload” but we cannot “overridden”
|
Can be inherited
|
Will automatically invoke when an object is created
|
Method has called explicitly
|
Q)
Garbage collection
G.C is also called
automatic memory management as JVM automatically removes the unused
variables/objects (value is null) from the memory. User program cann't directly
free the object from memory, instead it is the job of the garbage collector to
automatically free the objects that are no longer referenced by a program.
Every class inherits finalize() method from java.lang.Object, the
finalize() method is called by garbage collector when it determines no more
references to the object exists. In Java, it is good idea to explicitly assign null
into a variable when no more in use, calling System.gc() and Runtime.gc(),
JVM tries to recycle the unused objects, but there is no guarantee when all
the objects will garbage collected. Garbage collection is a low-priority
thread.
G.C is a low priority thread in
java, G.C cannot be forced explicitly. JVM may do garbage collection if it is
running short of memory. The call System.gc() does NOT force the garbage
collection but only suggests that the JVM may make an effort to do garbage
collection.
Q) How an
object becomes eligible for Garbage Collection?
A) An object is eligible for
garbage collection when no object refers to it, An object also becomes eligible
when its reference is set to null. The objects referred by method variables or
local variables are eligible for garbage collection when they go out of scope.
Integer i = new Integer(7);
i = null;
i = null;
Q) Final, Finally, Finalize
Final: - When we declare a sub class a final the
compiler will give error as “cannot subclass final class” Final to prevent
inheritance and method overriding. Once to declare a variable as final it
cannot occupy memory per instance basis.
à Final class cannot have static methods
à
Final class
cannot have abstract methods (Because of final class never allows any
class to inherit it)
à Final class can have a final method.
Finally: - Finally
create a block of code that will be executed after try catch block has
completed. Finally block will execute whether or not an
exception is thrown. If an exception is thrown, the finally block will execute
even if no catch statement match the exception. Any time a method is about to
return to the caller from inside try/catch block, via an uncaught exception or
an explicit return statement, the finally clause is also execute.
Using System.exit()
in try block will not allow finally code to execute
Finalize: - some
times an object need to perform some actions when it is going to destroy, if an
object holding some non-java resource such as file handle (or) window character
font, these resources are freed before the object is going to destroy.
Q) Can we declare
abstract method in final class?
A) It indicates an error to declare abstract
method in final class. Because of final class never allows any class to inherit
it.
Q) Can we declare final method in abstract
class?
A) If a method is defined as final then we
can’t provide the reimplementation for that final method in it’s derived
classes i.e overriding is not possible for that method. We can declare final
method in abstract class suppose of it is abstract too, then there is no used
to declare like that.
Q) Superclass & Subclass
A
super class is a class that is inherited whereas subclass is a class that does
the inheriting
Q) How will u implement 1) polymorphism 2) multiple
inheritance 3) multilevel inheritance in java?
A) Polymorphism – overloading and overriding
Multiple
inheritances – interfaces.
Multilevel inheritance – extending class.
No comments:
Post a Comment