Can we explicitly inherit from the Object class?
Whenever we declare a new class without an explicit
"extends" keyword, the class implicitly extends the fundamental
Object class.
When
you explicitly extend a different class that will be descended from the Object
class at some point in its hierarchy. It may help to understand the inheritance
structure more clearly if you explicitly extend the Object class.
What are the two
parts of an object?
Answer:
An object is
associated with data and functions.
Object = Data + Methods
Name the
different types of class members available in a Java program.
Answer:
There are two types of class members
available in a Java program-
• Instance members
• Static members
How do we create
an object?
Answer:
Objects
are created through new operator.
For example,
XYZ
obj = new XYZ ();
The above statement will create an object named “obj”.
Why do we use
methods?
Answer:
We use methods:
>To cope up with
complex problems
>To hide low-level details that
can confuse us
> To reuse portions of code
What is the
difference between user-defined data type and an application?
Answer: The difference is that there
is the main function in an application whereas a user defined data type does
not implement main() method.
What does it mean
that a method or field is “static”?
Answer:
The method with “static” keyword is a method that belongs to a class as a whole and not to a particular
instance or object. The static methods are also known as class methods.
To call a static method, we need to use it with the
classname as follows:
classname.methodname();
What is the
difference between the instance variables and class variables? Can you specify
the instance variable and the class variable in the example given below?
Answer:
When a number of objects are created from
the same class, each of them have their own distinct copies of instance variables. Every instance of the class
shares a class variable, which is in one
fixed location in memory.
Any object can change the value of a class variable. Here, x is a class
variable and the variable y is an instance variable
A class
"abc" inherits from a class "xyz". Write the syntax to
define the class "abc".
Answer:
class abc extends xyz
{
// definition of data members
// definition of member functions
}
How do you
prevent a subclass from having access to a member of a superclass?
Answer: To prevent a subclass from having access to a superclass member,
declare the member as private.
Explain the
relation between base class and derived class.
Answer: A
derived class is formed from the base class. It can use its methods and
variables (depend on specifier) and allow reusability. In a subclass, you can
add new methods and new fields.
More Q&A
How can a class be used as a user-defined data type?
Answer:
A
class is defined as user-defined data type in java because it is created by a
user. A class is based on fundamental data types.
For example, we have defined a class Demo having methods getdata() and
display(). Also,
we have created an object ob1 of Demo class.
class Demo {
int
a;
float
b;
char
c;
void
getdata() {
: :
}
void
display() {
: :
}}
Demo
obj1= new Demo();
Demo is the class with some fields and functions. These attributes
and functions of class are defined by the user.
Define base
class.
Answer: It
is the existing class from which a new class can be derived. It remains the
same. A base class is also known as super class/parent class.
Define the use of
implement keyword.
Answer: A
class can implement an interface using the keyword implements.
Define the
signature of a method.
Answer:
The signature of a Java method is the combination of method name along with
number and type of the parameters.
How does
constructor and destructor work in inheritance?
Answer:
When the object of derived class is created as constructor of base class, it
invokes the constructor of base class first and then the constructor of derived
class. When the object of derived class is destroyed, destructor of derived
class is invoked first, then the destructor of the base class.
How is an
interface different from a class?
Answer:
•
You cannot instantiate an interface.
• An interface doesn't contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields that can appear
in an interface must declare both static and final.
• It cannot be extended by a class but it can be implemented by a class.
• An interface can extend multiple interfaces.
What’s wrong with
the following constructor definition for the class student?
public void
student(int n)
{
rollno = n;
}
Answer:
Here, student is a constructor and a constructor does not have a return type.
It cannot be even declared as void.