Unit II – INHERITANCE AND INTERFACES
1. What is finalize () method?
The finalize() method is called just before an object is garbage collected. It is used to dis-
pose system resources, perform clean-up activities and minimize memory leaks.
Syntax:
protected void finalize() // finalize() is called just once on an object
{
………………
}
2. What is meant by Inheritance?
Inheritance is the mechanism in java by which one class is allow to inherit the features
(fields and methods) of another class. It is process of deriving a new class from an exist-
ing class.
Syntax:
class Subclass-name extends Superclass-name
{
//methods and fields
}
3. state the use of keyword super.
It an be used to refer immediate parent class instance variable when both parent and
child class have member with same name
It can be used to invoke immediate parent class method when child class has
overridden that method.
super() can be used to invoke immediate parent class constructor.
4. What is the use of Inheritance and what are its advantages?
Inheritance is the process of inheriting all the features from a class. The advantages of
inheritance are reusability of code and accessibility of variables and methods of the super
class by subclasses.
5. Write short notes on Object class.
The Object class is the parent class of all the classes in java by default (directly or indi-
rectly). The [Link] class is the root of the class hierarchy. Some of the Object
class are Boolean, Math, Number, String etc
6. Define abstract class?
Abstract classes are classes from which instances are usually not created. It is basically
used to contain common characteristics of its derived classes. Abstract classes generally
act as super classes. Methods can also be declared as abstract. This implies that non-
abstract classes must implement these methods.
7. When to use Abstract Methods & Abstract class?
Abstract methods are usually declared where two or more subclasses are expected to do a
similar thing in different ways through different implementations. These subclasses
extend the same Abstract class and provide different implementations for the abstract
methods.
Abstract classes are used to define generic types of behaviors at the top of an object-
oriented programming class hierarchy, and use its subclasses to provide implementation
details of the abstract class.
8. List out the rules in defining abstract classes.
An abstract class may have concrete (complete) methods.
An abstract class may or may not have an abstract method. But if any class has one or
more abstract methods, it must be compulsorily labeled abstract.
Abstract classes can have Constructors, Member variables and Normal methods.
Abstract classes are never instantiated.
For design purpose, a class can be declared abstract even if it does not contain any
abstract methods.
Reference of an abstract class can point to objects of its sub-classes thereby
achieving run-time polymorphism Ex: Shape obj = new Rectangle();
A class derived from the abstract class must implement all those methods that are
declared as abstract in the parent class.
If a child does not implement all the abstract methods of abstract parent class, then
the child class must need to be declared abstract as well.
9. Define final class.
When a class is declared with final keyword, it is called a final class. A final class cannot
be extended (inherited).
10. What are the uses of a final class?
To prevent inheritance, as final classes cannot be extended. For example, all Wrapper
Classes like Integer, Float etc. are final classes. We cannot extend them.
To create an immutable class like the predefined String class. We cannot make a class
immutable without making it final.
11. What are the benefits of final keyword in Java?
Final keyword improves performance. Not just JVM can cache final variable but also
application can cache frequently use final variables.
Final variables are safe to share in multi-threading environment without additional
synchronization overhead.
Final keyword allows JVM to an optimized method, variable or class.
12. Is final method inherited?
Yes, final method is inherited but we cannot override it.
For Example:
class Bike{ final void
run()
{
[Link](“running...”);
}
}
public class Honda2 extends Bike
{
public static void main(String args[]){
new Honda2().run();
}
}
13. What is meant by interface?
An interface is a reference type in Java. It is similar to class. It is a collection of abstract
methods. Along with abstract methods, an interface may also contain constants, default
methods, static methods, and nested types.
14. List at least four methods provided by Object Class.
public final Class getClass() returns the Class class object of this object. The Class class
can further be used to get the metadata of this class.
public int hashCode() returns the hashcode number for this object.
public boolean equals(Object compares the given object to this object.
obj)
public String toString() returns the string representation of this object.
public final void notify() wakes up single thread, waiting on this object's monitor.
public final void wait() causes the current thread to wait for the specified milliseconds,
until another thread notifies (invokes notify() or notifyAll()
method).
protected Object clone() creates and returns the exact copy (clone) of this object.
15. Compare the interface and abstract class.
Abstract class Interface
1) Abstract class can have abstract and Interface can have only abstract methods. Since
non-abstract methods. Java 8, it can have default and static
methods also.
2) Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
16. What are the uses of interface?
Interfaces are used to implement abstraction.
Since java does not support multiple inheritance in case of class, it can be achieved
by using interface.
It is also used to achieve loose coupling.
17. Define nested interface.
An interface which is declared inside another interface or class is called nested inter-
face. They are also known as inner interface. Since nested interface cannot be accessed
directly, the main purpose of using them is to resolve the namespace by grouping related
interfaces (or related interface and class) together.
18. How will you find out the length of a string in java? Give an example?
The length ( ) method is used to find number of characters is string. For
example, String str=”Hello”;
[Link](“Length of string is “+[Link]( ));
19. What is inner class?
If the methods of the inner class can only be accessed via the instance of the inner class,
then it is called inner class.
20. What would you use to compare two string variables - the operator == or the
method equals()?
I’d use the method equals() to compare the values of the Strings and the = = to check if
two variables point at the same instance of a String object.
Part B question
1. Explain overriding methods and final methods in Java
2. Explain in detail the various types of inheritance with neat sketch and write a simple java
program for each type.
3. What are interfaces? Explain with an example how multiple inheritance is implemented
using interfaces
4. Make use of ArrayList for implementing the concept of Queue and stack operations.