OOP Java - IMP M 3
OOP Java - IMP M 3
rd
3 SEM Exam – Important Questions
MODULE – 3
1. Interface Declaration:
An interface in Java is declared using the interface keyword, followed by the interface
name and the interface body containing method signatures (without method bodies).
2. Class Implementation:
To inherit behavior from multiple interfaces, a class can implement those interfaces by
providing implementations for all the methods declared in each interface.
The class uses the implements keyword followed by the names of the interfaces it wants to
implement.
3. Class Usage:
Once a class implements multiple interfaces, objects of that class inherit the behaviors
defined by all the interfaces it implements.
Through polymorphism, objects of the class can be treated as instances of any of the
interfaces it implements, allowing for flexibility and code reuse.
By allowing classes to implement multiple interfaces, Java enables the achievement of multiple
inheritances of behavior. This approach promotes code modularity, extensibility, and flexibility,
facilitating the design and implementation of complex systems. Additionally, interfaces also
support abstraction and the specification of contracts, making them a fundamental part of Java
programming.
3. Write a java program to implement multilevel inheritance with 3 levels of hierarchy.
Ans:
Here's a Java program demonstrating multilevel inheritance with 3 levels of hierarchy:
Explanation:
In this program, Animal is the parent class, Dog is the child class inheriting from Animal, and
Labrador is the grandchild class inheriting from Dog.
Animal class has a method eat(), Dog class has an additional method bark(), and Labrador class
has an additional method color().
The main() method creates an object of Labrador class and calls methods eat(), bark(), and
color() to demonstrate multilevel inheritance, where each subclass inherits from its immediate
superclass.
1. Single Inheritance:
Single inheritance involves a subclass inheriting properties and behaviors from a single
superclass.
In this type of inheritance, each class can have only one direct superclass.
It forms a hierarchical relationship among classes.
2. Multilevel Inheritance:
Multilevel inheritance involves a chain of inheritance with more than two levels of classes.
In this type of inheritance, a subclass inherits properties and behaviors from its superclass,
and another subclass further inherits from the previous subclass.
It forms a hierarchical tree structure among classes.
3. Hierarchical Inheritance:
Hierarchical inheritance involves multiple subclasses inheriting properties and behaviors
from a single superclass.
In this type of inheritance, a superclass has more than one subclass extending it.
It forms a hierarchical tree structure among classes.
6. What is meant by interface? State its need and write syntax and features of interface.
Ans:'
An interface in Java is a reference type, similar to a class, that specifies a set of abstract methods
(methods with no body) that a class implementing the interface must provide. In addition to
abstract methods, an interface can also contain constants (variables that are implicitly public, static,
and final) and default methods (methods with a default implementation provided in the interface
itself).
Abstraction: Interfaces allow for the definition of abstract behaviors or capabilities without
specifying the implementation details. This promotes abstraction and helps in achieving loose
coupling between components in a system.
Multiple Inheritance: Java does not support multiple inheritance of classes due to ambiguity
issues, but it does support multiple inheritance through interfaces. Interfaces enable a class to
inherit from multiple sources, facilitating code reuse and flexibility.
Contract Specification: Interfaces serve as a contract between classes, defining a set of methods
that implementing classes must adhere to. This ensures consistency and interoperability among
different classes and components.
Syntax:
Features of Interface:
In Java, overridden methods allow for runtime polymorphism, which is essential in object-oriented
programming. Here's how overridden methods support polymorphism:
Here's an illustration of the importance of the super keyword with a suitable example:
Explanation:
The Shape class represents a geometric shape and contains a color attribute and a display()
method to print the color.
The Rectangle class extends Shape and represents rectangles. It adds width and height attributes
and overrides the display() method to include width and height information.
In the constructor of Rectangle, super(color) is used to call the superclass constructor and
initialize the color attribute.
In the display() method of Rectangle, super.display() is used to invoke the superclass
display() method before printing width and height.
The calculateArea() method calculates and returns the area of the rectangle.
In the main() method, we create a Rectangle object and demonstrate the use of super to access
superclass constructor and methods.
Vehicle is the parent class with a method move() that prints "Vehicle is moving".
Car and Bike are subclasses of Vehicle. They override the move() method to provide specific
implementations.
In the main() method, we create objects of Car and Bike, but we use references of the superclass
Vehicle to refer to them.
This demonstrates polymorphism, where the move() method of the subclass is invoked based on
the actual object type at runtime.
The output shows that despite using a reference of the superclass, the overridden method of the
respective subclass is executed, illustrating polymorphic behavior.
1. Syntax of Nested Interface: An interface can be declared within another interface or a class. The
syntax for declaring a nested interface is straightforward:
1. Access Modifiers:
Nested interfaces can have any access modifier (public, protected, default, private) just like
other interface members.
The access modifier determines the accessibility of the nested interface within and outside
its containing interface or class.
2. Scope and Visibility:
A nested interface is scoped within its containing interface or class.
Outside the containing interface or class, the nested interface is accessed using the dot
notation: OuterInterface.NestedInterface.
3. Encapsulation and Organization:
Nesting interfaces helps in organizing related functionalities together.
It promotes encapsulation by hiding inner interfaces from outside classes unless they are
explicitly accessed through the outer interface.
4. Example:
In this example, Engine is a nested interface within the Vehicle interface. It defines the behavior of
an engine. The Car class implements both the Vehicle interface and its nested Engine interface,
providing specific implementations for their methods.
In summary, nesting interfaces in Java allows for better organization, encapsulation, and structuring
of code by defining related functionalities within the scope of a containing interface or class. It
helps in achieving a more modular and maintainable design.
12. What is abstract class and abstract method? Explain with example.
Ans:
In Java, an abstract class is a class that cannot be instantiated directly. It serves as a blueprint for
other classes and may contain both abstract and concrete methods. An abstract method is a
method declared in an abstract class but does not have an implementation. Subclasses of the
abstract class must provide implementations for all abstract methods, making them concrete.
Shape is an abstract class with an abstract method calculateArea() to calculate the area of a
shape and a concrete method display() to display generic shape information.
Rectangle and Circle are concrete subclasses of Shape that provide implementations for the
calculateArea() method specific to rectangles and circles.
In the main() method, we create objects of Rectangle and Circle classes and call their
calculateArea() methods to calculate the respective areas.
We also call the display() method on these objects, demonstrating that concrete methods can be
invoked directly from the abstract class.
13. Dynamic Method dispatch [EXTRAS]
Dynamic method dispatch is a mechanism in Java where the method to be executed is determined
at runtime rather than compile time. It enables polymorphism, allowing a subclass object to be
treated as an object of its superclass. This allows for more flexible and dynamic behavior, facilitating
code reuse and improving the maintainability of programs.
1. Polymorphism:
Dynamic method dispatch is closely related to polymorphism, which is the ability of a
reference variable of a superclass to refer to an object of its subclass.
This allows for the invocation of overridden methods from the subclass, even though the
reference variable is of the superclass type.
2. Runtime Binding:
During runtime, Java determines which method to execute based on the type of object the
reference variable points to, rather than the type of the reference variable itself.
This process is called runtime binding or late binding.
3. Example:
Dynamic method dispatch enables more flexible and polymorphic behavior in Java programs,
allowing for better code organization and reuse. It is a fundamental feature of object-oriented
programming languages like Java.
14. Default Interface Methods
Ans:
Default interface methods were introduced in Java 8 to provide a way to add new functionality to
existing interfaces without breaking the classes that implement them. Here's an explanation of
default interface methods:
1. Introduction:
Prior to Java 8, interfaces could only declare abstract methods, which had to be
implemented by classes that implemented the interface.
Default interface methods allow interfaces to declare concrete methods with a default
implementation.
2. Syntax:
Default methods are declared using the default keyword followed by the method signature
and implementation.
They are similar to regular interface methods but with a default implementation provided.
3. Usage:
Default methods enable backward compatibility by allowing interfaces to evolve over time
without forcing existing implementations to change.
Classes that implement the interface can choose to override default methods if needed.
4. Example:
In this example, Animal is an interface with an abstract method makeSound() and a default
method sleep().
The Dog class implements the Animal interface and provides an implementation for the
makeSound() method.
Since sleep() has a default implementation in the interface, Dog does not need to provide
its own implementation.
When dog.sleep() is called, it invokes the default implementation of sleep() provided by
the Animal interface.
Default interface methods are useful for extending interfaces without affecting existing
implementations, enabling better code maintenance and evolution. They provide a way to add new
functionality to interfaces in a backward-compatible manner.
1. Introduction:
Prior to Java 8, interfaces could only contain abstract method declarations.
Static methods in interfaces allow for the declaration of methods that are associated with
the interface itself, rather than with any specific instance of the interface.
2. Syntax:
Static methods are declared using the static keyword, similar to static methods in classes.
They must provide an implementation within the interface.
3. Usage:
Static methods can be called directly on the interface, without the need for an instance of
the interface.
They are useful for providing utility methods or helper functions related to the interface.
Example:
Explanation: