CHAPTER-2 7HRS 14 MARKS
MULTIPLE INHERITANCE INTERFACE ANDC PACKAGES
2MARKS
1.what is meant by inheritance?
Ans: is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system). inheritance in Java is that you
can create new classes that are built upon existing classes. When you inherit from an existing class,
you can reuse methods and fields of the parent class.
2.what is finalize() method?
Ans: The finalize() method is a protected method of the Object class that can be overridden by
subclasses. It is called by the garbage collector when it determines that there are no more references
to an object, allowing the object to perform cleanup before it is destroyed.
3.list the type of inheritance?
Ans: Single Inheritance: A class inherits from one superclass.
Multilevel Inheritance: A class is derived from another class, which is also derived from another
class.
4. how do you implement multiple inheritance in java?
Ans: multiple inheritance (where a class inherits from more than one class) is not supported directly
due to ambiguity issues, commonly known as the "diamond problem." However, you can achieve a
form of multiple inheritance using interfaces. Here’s how you can implement it:
Using Interfaces
1. Define Multiple Interfaces: Create multiple interfaces that declare the methods you want to
implement.
2. Implement Interfaces in a Class: A single class can implement multiple interfaces, providing
the necessary method definitions.
5. Define super class and subclass?
Ans: A superclass (or parent class) is a class that is extended by one or more subclasses. It can
provide common properties (fields) and behaviors (methods) that can be inherited by its subclasses.
A subclass (or child class) is a class that extends a superclass. It inherits the properties and methods
of the superclass, allowing it to reuse code and add additional features or override existing
behaviors.
6.State the use of Keyword super?
Ans: Accessing overridden methods from the superclass.
Invoking the superclass constructor to ensure proper initialization.
Distinguishing between fields in the subclass and superclass when they have the same name.
7.Define abstract class?
Ans: An abstract class in Java is a class that cannot be instantiated on its own and is intended to be
subclassed.
It can contain abstract methods (methods without a body) as well as concrete methods.
Abstract classes are used to define a common base with shared behavior and properties for derived
classes.
5MARKS
1.explain final()method with an example?
Ans: In Java, the finalize() method is a protected method of the Object class. It is called by the
garbage collector when it determines that there are no more references to an object, allowing the
object to perform cleanup actions before it is destroyed.
Purpose of finalize()
The primary purpose of the finalize() method is to allow an object to release resources (like closing
files or network connections) or perform any cleanup before the object is garbage collected.
However, the use of finalize() has been largely discouraged in modern Java programming due to un
Syntax
protected void finalize() throws Throwable {
// Cleanup code
super.finalize(); // Call to the superclass's finalize method
EXAMPLE: class Resource {
// Constructor
public Resource() {
System.out.println("Resource created.");
// Overriding the finalize() method
@Override
protected void finalize() throws Throwable {
try {
System.out.println("Resource is being cleaned up.");
} finally {
super.finalize(); // Call to superclass's finalize method
}
}
public class Main {
public static void main(String[] args) {
// Creating a Resource object
Resource res = new Resource(); // Output: Resource created.
// Making the Resource object eligible for garbage collection
res = null;
// Suggesting JVM to perform garbage collection
System.gc(); // This may not guarantee immediate execution
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
} } }
Output: Resource created.
Resource is being cleaned up.
2.Write Short Note on Package?
Ans: A package in Java is a namespace that organizes a set of related classes and interfaces. Packages
serve as a way to group similar classes, helping to manage large software projects and avoid naming
conflicts.
Key Features
1. Namespace Management: Packages allow for the creation of unique names for classes. For
instance, you can have multiple classes named User in different packages without any
conflict.
2. Code Organization: By grouping classes and interfaces, packages provide a structured
approach to organizing code, making it easier to locate and maintain.
3. Access Protection: Packages offer access control. Classes within the same package can access
each other's package-private members, promoting better encapsulation.
4. Reusability: Code within packages can be reused across different projects, which encourages
modular programming.
5. Standard Libraries: Java comes with a rich set of built-in packages (e.g., java.lang, java.util,
java.io) that provide essential classes for various functionalities, such as string manipulation,
data structures, and input/output operations.
3.Explain about method Overloading?
Ans: Method overloading is a feature in Java that allows a class to have more than one method with
the same name, as long as their parameter lists are different. This can be achieved by varying the
number of parameters, their types, or both.
Advantages of Method Overloading
1. Improved Readability: Method overloading makes code more readable by allowing the same
method name to perform similar operations with different types or numbers of parameters.
2. Flexibility: It provides flexibility to programmers to implement methods with different
parameter types without needing to create entirely new method names.
3. Easier Maintenance: It simplifies maintenance since a single method name is used for
related operations, making the code cleaner and easier to manage.
4.Explain about super keyword with an example?
Ans: The super keyword in Java is a reference variable that is used to refer to the immediate parent
class of the current object. It serves several purposes in inheritance, primarily to access methods,
fields, and constructors from the superclass.
Example: // Superclass
class Animal {
String name;
// Constructor
Animal(String name) {
this.name = name; // Initialize the name field
// Method to display sound
void sound() {
System.out.println("Animal makes a sound");
// Subclass
class Dog extends Animal {
// Constructor
Dog(String name) {
super(name); // Call the superclass constructor
// Overriding the sound method
@Override
void sound() {
super.sound(); // Call the superclass method
System.out.println("Dog barks"); // Additional behavior
// Main class
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.sound(); // Calls the overridden method
System.out.println("Dog's name is: " + dog.name); // Access superclass field
} }
OUTPUT: Animal makes a sound
Dog barks
Dog's name is: Buddy
5.what is Polymorphism? explain with Example?
Ans: Polymorphism is a fundamental concept in object-oriented programming that allows methods
to perform differently based on the object that is calling them. In Java, polymorphism enables a
single interface to be used for different underlying data types.
There are two main types of polymorphism:
1.Compile-time Polymorphism.
2. Runtime Polymorphism.
EXAMPLE: class MathOperations {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
// Method to add three integers
int add(int a, int b, int c) {
return a + b + c;
// Method to add two doubles
double add(double a, double b) {
return a + b;
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println("Sum of two integers: " + math.add(5, 10)); // Calls first method
System.out.println("Sum of three integers: " + math.add(5, 10, 15)); // Calls second method
System.out.println("Sum of two doubles: " + math.add(5.5, 10.5)); // Calls third method
} }
OUTPUT: Sum of two integers: 15
Sum of three integers: 30
Sum of two doubles: 16.0
6.what is difference between method overloading and method overriding?
Ans:
Method Overloading Method Overriding
1.Multiple methods with the same name but A subclass provides a specific implementation of a
different parameter lists within the same class. method already defined in its superclass.
2.To allow the same method name to perform To define specific behavior in the subclass while
different tasks based on input parameters. using the same method name as in the superclass.
3.Compile-time polymorphism Runtime polymorphism.
4.Must differ in type, number, or order of Must have the same name and parameter list as in the
parameters. superclass.
5.Can have different return Must have the same return type (or a subtype) as the
types. overridden method.
6.int add(int a, int b), double add(double a, void display() {} in superclass and void display() {} in
double b). subclass.
10MARKS
1.what are Interface? Explain with an Example how multiple inheritance is implemented using
interface?
Ans: An interface in Java is a reference type that defines a set of abstract methods that a class must
implement. Interfaces are used to achieve abstraction and to support multiple inheritance, allowing
a class to inherit behavior from multiple sources.
Characteristics of Interfaces:
1. Abstract Methods.
2. Constants.
3. Multiple Inheritance.
4. Default and Static Methods.
Here’s an example that demonstrates how multiple inheritance is implemented using interfaces.
// First Interface
interface Animal {
void eat(); // Abstract method
// Second Interface
interface Pet {
void play(); // Abstract method
// Class implementing multiple interfaces
class Dog implements Animal, Pet {
@Override
public void eat() {
System.out.println("Dog eats dog food.");
@Override
public void play() {
System.out.println("Dog plays fetch.");
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Output: Dog eats dog food.
myDog.play(); // Output: Dog plays fetch.
2.Explain Types of inheritance in java write a java program on single inheritance?
Ans: Types of Inheritance in Java
Java supports several types of inheritance, which describe the relationships between classes. The
main types of inheritance are:
1. Single Inheritance: A subclass inherits from one superclass.
2. Multilevel Inheritance: A subclass inherits from a superclass, which in turn is a subclass of
another superclass (forming a chain).
3. Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
4. Multiple Inheritance (through interfaces): A class can implement multiple interfaces,
effectively allowing it to inherit from multiple sources, but Java does not support multiple
inheritance with classes to avoid ambiguity.
Single Inheritance Example
Here’s a simple example demonstrating single inheritance in Java:
// Superclass
class Animal {
void eat() {
System.out.println("Animal eats.");
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
// Main class
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog(); // Create an instance of Dog
myDog.eat(); // Call method from the superclass
myDog.bark(); // Call method from the subclass
------------------------------------END--------------------------------------------------------------------