0% found this document useful (0 votes)
25 views

OOP Java - IMP M 3

Uploaded by

sunilgowda88849
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

OOP Java - IMP M 3

Uploaded by

sunilgowda88849
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

OOPs in JAVA

rd
3 SEM Exam – Important Questions

MODULE – 3

1. Which are the restrictions present for static declared methods?


Ans:
Static methods in Java come with certain restrictions due to their nature. Here are the main
restrictions for static declared methods:

1. Access to Instance Variables:


 Static methods cannot directly access instance variables or instance methods of the class.
They can only access static variables and other static methods.
 Attempting to access instance variables or instance methods from a static method will result
in a compilation error unless they are accessed through an object reference.
2. This Keyword:
 Static methods cannot use the this keyword to refer to the current instance of the class
because they are not associated with any particular instance. The this keyword is only
applicable to instance methods.
3. Overriding:
 Static methods cannot be overridden in subclasses with a non-static method. If a subclass
defines a static method with the same signature as a static method in the superclass, it is
considered as method hiding, not overriding.
 However, a static method in a superclass can be hidden by another static method with the
same signature in a subclass.
4. Super Keyword:
 Similar to the this keyword, static methods cannot use the super keyword to call superclass
methods or access superclass variables because they are not associated with any instance.
5. Inheritance and Polymorphism:
 Static methods are not polymorphic. This means that the method invoked is determined by
the reference type, not the object type, and cannot be overridden in subclasses.
6. Cannot be Abstract:
 Static methods cannot be declared as abstract since they are associated with the class itself
rather than instances of the class.
2. Explain how interface is used to achieve multiple Inheritances in Java.
Ans:
In Java, interfaces are used to achieve multiple inheritances by allowing a class to implement
multiple interfaces. Unlike classes, which support single inheritance (a class can only inherit from
one superclass), a class in Java can implement multiple interfaces, thereby inheriting multiple sets
of behaviors or capabilities.

Here's how interfaces are used to achieve multiple inheritances in Java:

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.

4. Write a java program to extend interface assuming suitable data.


Ans:
In Java, interfaces can extend other interfaces, similar to how classes can extend other classes.
Here's an example Java program demonstrating extending an interface:
Explanation:

 In this program, Shape is the base interface with a method draw().


 ThreeDimensionalShape is an interface extending Shape interface and adds another method
calculateVolume().
 Cube class implements ThreeDimensionalShape interface and provides implementations for both
draw() and calculateVolume() methods.
 In the main() method, we create an object of Cube class and call methods draw() and
calculateVolume() to demonstrate interface extension.

5. What are Types of Inheritance?


Ans:
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new
class (subclass or derived class) to inherit properties and behaviors (methods) from an existing class
(superclass or base class). There are several types of inheritance, each serving different purposes.
Here are the main types of inheritance:

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).

Need for Interfaces:

 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:

7. Explain inheritance and polymorphism features of Java.


Ans:
Inheritance:
 Definition: Inheritance is a fundamental feature of object-oriented programming (OOP) that
allows a new class (subclass or derived class) to inherit properties and behaviors (methods)
from an existing class (superclass or base class).
 Mechanism: The subclass inherits the members (fields and methods) of its superclass,
allowing for code reuse and promoting the concept of "is-a" relationship between classes.
 Syntax: In Java, inheritance is achieved using the extends keyword. A subclass extends a
superclass, and it can access superclass members using the super keyword.
 Example:
Polymorphism:
 Definition: Polymorphism is another key feature of OOP that allows objects of different
classes to be treated as objects of a common superclass, enabling flexibility and extensibility
in code design.
 Types:
 Compile-time Polymorphism (Method Overloading): Involves multiple methods
with the same name but different parameter lists in the same class.
 Run-time Polymorphism (Method Overriding): Involves a subclass providing a
specific implementation of a method defined in its superclass.
 Mechanism: Polymorphism allows methods to be called based on the actual object type at
runtime, rather than the reference type, facilitating dynamic method dispatch.
 Example:

8. Explain method overriding with suitable example.


Ans:

In Java, overridden methods allow for runtime polymorphism, which is essential in object-oriented
programming. Here's how overridden methods support polymorphism:

1. Facilitating Polymorphism: Overridden methods enable Java to support runtime polymorphism.


Polymorphism is crucial because it allows a general class to define methods common to all its
subclasses, while the subclasses can provide specific implementations of those methods.
2. One Interface, Multiple Methods: Overridden methods contribute to the "one interface, multiple
methods" aspect of polymorphism. They allow a superclass to define methods that all its subclasses
inherit, while still permitting subclasses to override and customize those methods as needed.
3. Hierarchy of Specialization: Inheritance hierarchies in Java move from lesser to greater
specialization. Superclasses provide elements that subclasses can use directly and define methods
that subclasses must implement. This structure provides flexibility to subclasses while enforcing a
consistent interface.
4. Combining Inheritance with Overridden Methods: By combining inheritance with overridden
methods, a superclass can define the general form of methods used by all its subclasses. This
approach allows for code reuse and robustness in object-oriented design.
OUTPUT
9. What is importance of super keyword in inheritance? Illustrate with suitable example.
Ans:
The super keyword in Java is used to refer to the immediate parent class object. It is primarily used
in inheritance to access methods and variables of the superclass. The importance of the super
keyword lies in its ability to invoke superclass constructors, methods, and variables, thereby
facilitating code reuse and enhancing the flexibility of the inheritance mechanism.

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.

10. Write a single program to implement inheritance and polymorphism in java.


Explanation:

 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.

11. Explain concept of nesting of interface.


Ans:
In Java, interfaces can be nested within other interfaces or classes. This concept is known as nesting
of interfaces. Nesting interfaces allows for better organization and encapsulation of related
functionalities. Here's a breakdown of the concept:

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.

Here's an explanation with an example:


Explanation:

 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.

Here's an explanation of dynamic method dispatch:

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:

 In this example, Animal is the superclass and Dog is the subclass.


 We create an object of the Dog class and assign it to a reference variable of type Animal.
 When animal.makeSound() is called, Java dynamically determines that it refers to the
overridden makeSound() method in the Dog class, and thus the output is "Dog barks".

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.

15. Static methods in an interface


Ans:
Static methods in an interface were introduced in Java 8 along with default methods. They allow
interfaces to provide utility methods that are not tied to any specific instance of the interface.
Here's an explanation of static methods in an interface:

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:

 TestInterface is an interface containing an abstract method square(int a) and a static method


show().
 The TestClass implements TestInterface and provides an implementation for the square(int
a) method.
 In the main() method, we create an object of TestClass and call its square() method to print the
square of 4.
 We also call the static method show() of the interface TestInterface, demonstrating the
invocation of a static method from an interface.

You might also like