<-Pre | Home | Next-> |
Is it possible for a private method of a superclass be declared within a subclass? *IMP*
Yes. A private field or method or inner class belongs to its declared class and hides from its subclasses.There is no way for private field or method or inner class to have a runtime overloading or overriding (polymorphism) features.
What is the default value of an object reference declared as an instance variable?
null. unless we define it explicitly.
What is the difference between a constructor and a method?
A constructor is a member function of a class that is used to create objects of that class, invoked using the new operator. It has the same name as the class and has no return type.
They are only called once, whereas member functions can be called many times. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.
How can a subclass call a method or a constructor defined in a superclass?
super.method(); is used to call a super class method from a sub class. To call a constructor of the super class, we use the super(); statement as the first line of the subclass’s constructor.
Can a top-level class be private or protected?
No. A top-level class cannot be private or protected. It can have either “public” or no modifier. If it does not have a modifier it is supposed to have a default access. If a top level class is declared as private/protected the compiler will complain that the “modifier private is not allowed here”.
Where and how can you use a private constructor?
Private constructor can be used if you do not want any other class to instantiate the class. This concept is generally used in Singleton Design Pattern. The instantiation of such classes is done from a static public method.
How are this() and super() used with constructors?
this() is used to invoke a constructor of the same class.
super() is used to invoke a superclass constructor.
What is Overriding? *IMP*
subclass inherits a method from a superclass and subclass redefine that method keeping name, return type and argument same as that of superclass method is called overrding.
Restrictions placed on method overriding.
Overridden methods must have the same name, argument list, and return type.What are the Object and Class classes used for?
The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program. The Class class is used to obtain information about an object’s design. A Class is only a definition or prototype of real life object. Whereas an object is an instance or living representation of real life object. Every object belongs to a class and every class contains one or more related objects.
Differentiate between a Class and an Object?
A Class is only a definition or prototype of real life object. Whereas an object is an instance or living representation of real life object. Every object belongs to a class and every class contains one or more related objects.
Which class should you use to obtain design information about an object?
The Class class is used to obtain information about an object’s design.
What is a singleton class?
A class whose number of instances that can be instantiated is limited to one is called a singleton class.
Does a class inherit the constructors of its superclass?
A class does not inherit constructors from any of its super classes.
Can an object’s finalize() method be invoked while it is reachable?
An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.
What is the use of Runtime class?
It returns the runtime information like amount of free memory availability,execute command,number of processors available to the Java virtual machine etc.
What is the purpose of the System class?
The purpose of the System class is to provide access to system resources.
Can an unreachable object become reachable again?
An unreachable object may become reachable again. This can happen when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable object.
Why default constructor of base class will be called first in java?
A subclass inherits all the methods and fields (eligible one) from the base class, so base class is constructed in the process of creation of subclass object (subclass is also an object of superclass). Hence before initializing the default value of sub class the super class should be initialized using the default constructor.
What are the other ways to create an object other than creating as new object? *IMP*
1.new operator
2.class.forName: Classname obj = Class.forName(”Fully Qualified class Name”).newInstance();
3.newInstance
4.object.clone
Next -> Abstract class and Interface |
*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****
No comments:
Post a Comment