You are on page 1of 7

K.L.N.

COLLEGE OF ENGINEERING
DEPARTMENT OF INFORMATION TECHNOLOGY
QUESTION BANK –R 2017
CS8392-OBJECT ORIENTED PROGRAMMING

UNIT II - INHERITANCE AND INTERFACES

1. What is a class hierarchy? Give Example.


The class hierarchy defines the inheritance relationship between the classes.  The root of the
class hierarchy is the class Object.  Every class in Java directly or indirectly extends (inherits
from) this class. 

2. State the uses of reflection.


Reflection is an API which is used to examine or modify the behavior of methods, classes,
interfaces at runtime.
 The required classes for reflection are provided under java.lang.reflect package.
 Reflection gives us information about the class to which an object belongs and also the
methods of that class which can be executed by using the object.
 Through reflection methods can be invoked at runtime irrespective of the access specifier
used with them.
Reflection can be used to get information about –
1. Class The getClass() method is used to get the name of the class to which an object
belongs.
2. Constructors The getConstructors() method is used to get the public constructors of the
class to which an object belongs.
3. Methods The getMethods() method is used to get the public methods of the class to which
an objects belongs.

3. What is object cloning?


The object cloning is a way to create exact copy of an object. The clone() method of Object class
is used to clone an object. The java.lang.Cloneable interface must be implemented by the class
whoseobject clone we want to create. The Cloneable interface defines no members. It is used to
indicate that a class allows a bitwise copy of an object (that is, a clone) to be made. If you try to
call clone( ) on a class that does not implement Cloneable, a CloneNotSupportedException is
thrown. When a clone is made, the constructor for the object being cloned is not called. A clone
is simply an exact copy of the original.
4. Define abstract class and give its properties.
A class which contains the abstract keyword in its declaration is known as abstract class.
 Abstract classes may or may not contain abstract methods, i.e., methods without body
( public void get(); )
 But, if a class has at least one abstract method, then the class must be declared abstract.
 If a class is declared abstract, it cannot be instantiated.
 To use an abstract class, you have to inherit it from another class, provide
implementations to the abstract methods in it.
 If an abstract class is inherited, implementations should be provided to all the abstract
methods in it.

5. Why does not java support destructor and how does the finalize method help in garbage
collection?
Java does not support destructor because it handles deallocation for you automatically. The
technique that accomplishes this is called garbage collection. when no references to an object
exist, that object is assumed to be no longer needed, and the memory occupied by the object can
be reclaimed.
Specific actions that will occur when an object is just about to be reclaimed by the garbage
collector. The Java run time calls that method whenever it is about to recycle an object of that
class. Inside the finalize( ) method, those actions that must be performed before an object is
destroyed is specified. The garbage collector runs periodically, checking for objects that are no
longer referenced by any running state or indirectly through other referenced objects. Right
before an asset is freed, the Java run time calls the finalize( ) method on the object. The
finalize() method has this general form:
protected void finalize( )
{
// finalization code here
}
6. Distinguish between static and dynamic binding.
Static binding Dynamic binding
The binding which can be resolved at When compiler is not able to resolve the
compile time by compiler is known as static call/binding at compile time, such binding is
or early binding.  known as Dynamic or late Binding. 
Binding of private, static and final methods When the method overriding is actually
always happen at compile time since these happening and the reference of parent type is
methods cannot be overridden.  assigned to the object of child class type then
such binding is resolved during runtime.
The binding of overloaded methods is static The binding of overridden methods is
dynamic.

7. How does declaration of a local inner class differ from declaration of an anonymous inner
class?
A local inner class can be declared within a block. This block can be either a method body,
initialization block, for loop or even an if statement.
Anonymous inner classes are defined at the same time they are instantiated with new. They are
not declared as local classes are done rather anonymous inner classes are defined in
the new expression itself, as part of a statement. The syntax of an anonymous class expression is
like the invocation of a constructor, except that there is a class definition contained in a block of
code.
8. Difference between static and inner(non-static nested) classes.
Static nested classes do not directly have access to other members(non-static variables and
methods) of the enclosing class because as it is static, it must access the non-static members
of its enclosing class through an object. That is, it cannot refer to non-static members of its
enclosing class directly. Because of this restriction, static nested classes are seldom used.
Non-static nested classes (inner classes) has access to all members(static and non-static
variables and methods, including private) of its outer class and may refer to them directly in
the same way that other non-static members of the outer class do.

9. Mention the purpose of ‘Final’ keyword.


 When a variable is declared with final keyword, its value can’t be modified,
essentially, a constant. 
 When a method is declared with final keyword, it is called a final method. A final
method cannot be overridden. 
 When a class is declared with final keyword, it is called a final class. A final class
cannot be extended(inherited). 

10. Where do we use interfaces and abstract classes ?


Consider using abstract classes if any of these statements apply to situation:
1. If code should be shared among several closely related classes.
2. If the classes that extend the abstract class have many common methods or fields or
require access modifiers other than public (such as protected and private).
3. If non-static or non-final fields are declared. This enables to define methods that can
access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to situation:
1. If unrelated classes are expected to implement the interface. For example, the
interfaces Comparable and Cloneable are implemented by many unrelated classes.
2. If the behavior of a particular data type wanted to be specified , but not concerned
about who implements its behavior.
3. If the program want to take advantage of multiple inheritances.

11. What is subclasser responsilbility?


Certain methods can be overridden by subclasses by specifying the abstract type modifier.
These methods are sometimes referred to as subclasser responsibility because they have no
implementation specified in the superclass. Thus, a subclass must override them—it cannot
simply use the version defined in the superclass.

12. How Super() is used with constructor?


A subclass can call a constructor defined by its superclass by use of the following form of
super:
super(arg-list);
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the
keyword super.
13. What are the two general forms of Super?
Super has two general forms. The first calls the superclass’ constructor. The second is used
to access a member of the superclass that has been hidden by a member of a subclass.

14. What is an inner class and Give an example for it.


An inner class is a non-static nested class. It has access to all of the variables and methods of
its outer class and may refer to them directly in the same way that other non-static members
of the outer class do.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}

15. Can an abstract class in java can be instantiated. Give reason.


There can be no objects of an abstract class. That is, an abstract class cannot be directly
instantiated with the new operator. Such objects would be useless, because an abstract class
is not fully defined. Also,we cannot declare abstract constructors, or abstract static methods.

16. Define nested class and list its types.


A class within another class are known as nested classes. A nested class has access to the
members, including private members, of the class in which it is nested. There are two types
of nested classes: static and non-static. A static nested class is one that has the static
modifier applied. As it is static, it cannot refer to non-static members of its enclosing class
directly. An inner class is a non-static nested class.

17. How java allows dynamic intialization of variables?


Initializing a variable at run time is called dynamic initialization. Java allows variables to be
initialized dynamically, using any expression valid at the time the variable is declared.
// Demonstrate dynamic initialization.
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
Three local variables—a, b, and c—are declared. The first two, a and b, are initialized by
constants. C is initialized dynamically to the length of the hypotenuse.
18. How to create one dimensional and multi dimensional array?
To create an array, create an array variable of the desired type. The general form of a one-
dimensional array declaration is
type var-name[ ];
type declares the element type (also called the base type) of the array. The element type
determines the data type of each element that comprises the array. Thus, the element
type for the array determines what type of data the array will hold.
Multidimensional arrays are actually arrays of arrays the following declares a
twodimensional array variable called twoD:
int twoD[][] = new int[4][5];

19. Write down the syntax for defining interface.


An interface is defined much like a class. This is a simplified general form of an interface:
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
//...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
20. How runtime polymorphism is achieved in java?
Method overriding is one of the ways in which Java supports Runtime Polymorphism.
Dynamic method dispatch is the mechanism by which a call to an overridden method is
resolved at run time, rather than compile time. When an overridden method is called through
a superclass reference, Java determines which version(superclass/subclasses) of that method
is to be executed based upon the type of the object being referred to at the time the call
occurs. Thus, this determination is made at run time.
21. What is the difference between static and non-static variables?
Non-Static variables Static variables
These variable should not be preceded by These variables are preceded by static
any static keyword Example: keyword.Example:
class A class A
{ {
int a; static int b;
} }
Memory is allocated for these variable Memory is allocated for these variable at the
whenever an object is created time of loading of the class.
Memory is allocated multiple time Memory is allocated for these variable only once
whenever a new object is created. in the program.
Non-static variable also known as Memory is allocated at the time of loading of
instance variable while because memory class so that these are also known as class
is allocated whenever instance is created. variable.
Non-static variable also known as Memory is allocated at the time of loading of
instance variable while because memory class so that these are also known as class
is allocated whenever instance is created. variable.
Non-static variable are specific to an Static variable are common for every object that
object means there memory location can be sharable by
every object reference or same class.
Non-static variable can access with object Static variable can access with class reference.
reference.

22. What is the difference between the String and StringBuffer classes?
String StringBuffer
String class is immutable. StringBuffer class is mutable.
String is slow and consumes more memory when StringBuffer is fast and consumes less
you concat too many strings because every time it memory when you cancat strings.
creates new instance.
String class overrides the equals() method of Object StringBuffer class doesn't override the
class. So you can compare the contents of two equals() method of Object class.
strings by equals() method.

23. What is an anonymous inner class?


Anonymous Inner Class is an inner class without a name and for which only a single object is
created. An anonymous inner class can be useful when making an instance of an object with
certain “extras” such as overloading methods of a class or interface, without having to
actually subclass a class.

24. Enumerate the situation in which static methods are used?


 When code or data that can be shared across all instances of the same class, then that
portion of code and data is declared as static method.
 They are basically used to access static field(s) of the class.

25. Define Object class and List the methods defined by it.
Object class is a special class, Object, defined by Java. All other classes are subclasses of
Object. Object is a superclass of all other classes. This means that a reference variable of
type Object can refer to an object of any other class.
Object defines the following methods, which means that they are available in every object.

26. What is the significance of the abstract class?


 Abstract class helps in generalisation of its methods when class extends an Abstract
class. Abstract classes are templates for future specific classes.
 Helps in code reusability.
 Abstract class also helps in defining the subclass features efficiently. 
 It allows code to be shared between classes of the same type.
 Abstract class allows group several related classes together as subclasses. Grouping
classes together is important in keeping a program organized and understandable.

27. Differentiate Class and interface.


Class Interface
A class is instantiated to create objects. An interface can never be instantiated as the
methods are unable to perform any action on
invoking.
The members of a class can be private, The members of an interface are always public
public or protected.
The methods of a class are defined to The methods in an interface are purely abstract.
perform a specific action.
A class can implement any number of An interface can extend multiple interfaces but can
interface and can extend only one class. not implement any interface.
A class can have constructors to initialize An interface can never have a constructor.
the variables.

28. Compare and contrast abstract class and interface.


abstract class Interface

Abstract class can have abstract and non-abstract Interface can have only abstract
methods. methods.
Abstract class doesn't support multiple inheritance. Interface supports multiple
inheritance.
Abstract class can have final, non-final, static and Interface has only static and final
non-static variables. variables.
Abstract class can provide the implementation of Interface can't provide the
interface. implementation of abstract class.
An abstract class can extend another Java class and An interface can extend another Java
implement multiple Java interfaces. interface only.
A Java abstract class can have class members like Members of a Java interface are
private, protected, etc. public by default.

You might also like