Beruflich Dokumente
Kultur Dokumente
Two methods may not have the same name and argument list but different return types.
String objects are immutable whereas StringBuffer objects are not. StringBuffer unlike Strings
support growable and modifiable strings.
Sure. A private field or method or inner class belongs to its declared class and hides from its
subclasses.
There is no way for private stuff to have a runtime overloading or overriding (polymorphism)
features.
Or
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.
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.
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”.
this() is used to invoke a constructor of the same class. super() is used to invoke a superclass
constructor.
When a class defines a method using the same name, return type, and argument list as that of a
method in its superclass, the method in the subclass is said to override the method present in the
Superclass. When the method is invoked for an object of the
class, it is the new definition of the method that is called, and not the method definition from
superclass.
Restrictions placed on method overriding
• Overridden methods must have the same name, argument list, and return type.
• The overriding method may not limit the access of the method it overrides. Methods may be
overridden to be more public, not more private.
• The overriding method may not throw any exceptions that may not be thrown by the overridden
method.
What are the Object and Class classes used for? Which class should you use to obtain
design information about an object?
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.
Or
This design pattern is used by an application to ensure that at any time there is only one instance
of a class created. You can achieve this by having the private constructor in the class and having
a getter method which returns an object of the class and creates one for the first time if its null.
Or
Method overloading: When 2 or more methods in a class have the same method names with
different arguments, it is said to be method overloading. Overloading does not block inheritance
from the superclass. Overloaded methods must have different method signatures
Method overriding : When a method in a class has the same method name with same
arguments as that of the superclass,
it is said to be method overriding. Overriding blocks inheritance from the superclass. Overridden
methods must have same signature.
If a class is declared without any access modifiers, where may the class be accessed?
A class that is declared without any access modifiers is said to have package or default access.
This means that the class can only be accessed by other classes and interfaces that are defined
within the same package.
A class does not inherit constructors from any of its super classes.
The EventObject class and the EventListener interface support event processing
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.
The purpose of the Runtime class is to provide access to the Java runtime system.
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.
I want to create two instances of a class ,But when trying for creating third instance it
should not allow me to create . What i have to do for making this?
test1()
{ cntr++;
if(cntr>2)
System.out.println("hello 1");
System.out.println("hello 2");
}
}
An Object May not have a class definition. eg int a[] where a is an array.
my is an instance.
It is a memory area which stores all the objects created by an executing program.
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?
1.new operator
3.newInstance
4.object.clone
Class: A class is a user defined data type with set of data members & member functions
Instance: This represents the values of data members of a class at a particular time