0% found this document useful (0 votes)
14 views10 pages

OOPP Unit 2 - QB

The document is a question bank focused on Object-Oriented Programming, specifically covering inheritance, abstract classes, and interfaces in Java. It includes definitions, examples, and explanations of key concepts such as method overloading, method overriding, the super keyword, and the differences between classes and interfaces. Additionally, it addresses the diamond problem in multiple inheritance and provides exercises for deeper understanding of the topics.

Uploaded by

Harish Raghave.G
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)
14 views10 pages

OOPP Unit 2 - QB

The document is a question bank focused on Object-Oriented Programming, specifically covering inheritance, abstract classes, and interfaces in Java. It includes definitions, examples, and explanations of key concepts such as method overloading, method overriding, the super keyword, and the differences between classes and interfaces. Additionally, it addresses the diamond problem in multiple inheritance and provides exercises for deeper understanding of the topics.

Uploaded by

Harish Raghave.G
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/ 10

23AD1303 - OBJECT ORIENTED PROGRAMMING PARADIGM

QUESTION BANK

UNIT II - INHERITANCE, ABSTRACT CLASSES AND INTERFACES


Overloading Methods - Inheritance: Basics – Types of Inheritance - Constructors and Inheritance
- Super keyword - Method Overriding – Dynamic Method Dispatch – Abstract Classes and
Methods – final keyword - Interfaces: Defining an interface – implementing an interface –
Multiple Inheritance through interface.
Part A
1 Differentiate between Class and Interface.
Class Interface
The keyword used to create a class is The keyword used to create an interface
“class” is “interface”
A class can be instantiated i.e., objects of An Interface cannot be instantiated i.e.
a class can be created. objects cannot be created.
Classes do not support multiple The interface supports multiple
inheritance. inheritance.
It can be inherited from another class. It cannot inherit a class.
It can be inherited by a class by using the
It can be inherited by another class using keyword ‘implements’ and it can be
the keyword ‘extends’. inherited by an interface using the
keyword ‘extends’.
It can contain constructors. It cannot contain constructors.
It cannot contain abstract methods. It contains abstract methods only.
2 Can we instantiate an abstract class? Justify
 Abstract class are classes which can have abstract methods and it can’t be instantiated.
We cannot instantiate an abstract class in Java because it is abstract, it is not complete,
hence it cannot be used.
 Java does not allow the direct instantiation of abstract classes. Hence it is not feasible
to create an instance of an abstract class.
 However, there are a few ways to create an instance of an abstract class indirectly, by
using a concrete subclass or an anonymous class.

Using a Concrete Subclass - The most common way to create an instance of an abstract
class is to use a concrete subclass. To do this, first create a concrete subclass that
implements all of the abstract methods in the abstract class. Following that, an instance
of this concrete subclass and use it in place of an instance of the abstract class.
Using an Anonymous Class - Using an anonymous class is another method for producing
an instance of an abstract class. An instance of an abstract class can be created using an

1
anonymous class, which is a class that is declared without a name.
3 Illustrate abstract class in Java with a real-life example.
An abstract class in Java is a class that cannot be instantiated on its own and may contain
abstract methods (methods without a body) that must be implemented by subclasses.
Example
abstract class Vehicle {
abstract void start(); // abstract method
void stop() {
System.out.println("Vehicle stopped.");
}}
class Car extends Vehicle {
void start() {
System.out.println("Car started with key.");
}}
4 Multiple Inheritance creates Diamond Problem Ambiguity. Explain?
The diamond problem is a classic illustration of the complexities associated with
multiple inheritance. It occurs in a scenario where a class inherits from two classes that
both inherit from a common base class. If both parent classes override a method from the
base class, and the child class does not provide its own implementation, it's unclear which
version of the method the child class should inherit. This ambiguity complicates method
resolution and can lead to unexpected behaviours.

2
The diamond problem is illustrated in this diagram with a more complex hierarchy.
Java's approach to this problem is to disallow multiple inheritance of classes, thus
preventing the formation of such diamond-shaped hierarchies. Java allows a class to
implement multiple interfaces but limits inheritance to a single parent class, effectively
avoiding the diamond problem and the associated complexities in method resolution.
5 How do Interface differ from Abstract Class?
Abstract class Interface
Abstract class can have abstract and non- Interface can have only abstract methods.
abstract methods.
It doesn’t support multiple Interface supports multiple inheritance.
inheritance.
Abstract class can have final, non- final, Interface has only static and
static and non-static variables. final variables.
Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
An abstract class can extend another Java An interface can extend another Java
class and implement multiple Java interface only.
interfaces.
A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.
Example: Example:
public abstract class Shape { public interface Drawable {
public abstract void draw(); void draw();
} }
6 How can you access the super class version of an overridden method? How?
When invoking a superclass version of an overridden method the super keyword is used.

Example
class Animal{
public void move() {
System.out.println("Animals can move");
}}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}}
public class TestDog {

3
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}}

Output:
Animals can move Dogs can walk and run
7 List the order of execution of constructor during creation of class Hierarchy.
While implementing inheritance in a Java program, every class has its own constructor.
Therefore, the execution of the constructors starts after the object initialization. It follows
a certain sequence according to the class hierarchy. There can be different orders of
execution depending on the type of inheritance.
1. Order of execution of constructor in Single inheritance
2. Order of execution of constructor in Multilevel inheritance
3. Calling same class constructor using this keyword
4. Calling superclass constructor using super keyword
8 Interpret Interface with its syntax.
 An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
 An interface is declared by using the interface keyword. It provides total abstraction;
means all the methods in an interface are declared with the empty body, and all the
fields are public, static and final by default. A class that implements an interface must
implement all the methods declared in the interface.

Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
9 How a subclass can call a constructor defined by its superclass?
 A subclass can call a constructor defined by its superclass by use of the following form
of super: super(parameter-list);
 Here, parameter-list specifies any parameters needed by the constructor in the
superclass. super( ) must always be the first statement executed inside a subclass
constructor.
10 Define Abstract Method() with its syntax?
 A method declared using the abstract keyword within an abstract class and does not

4
have a definition (implementation) is called an abstract method.
 When we need just the method declaration in a super class, it can be achieved by
declaring the methods as abstracts.
Abstract method is also called subclass responsibility as it doesn't have the
implementation in the super class. Therefore, a subclass must override it to provide the
method definition.
Syntax for abstract method
abstract return_type method_name( [ argument-list ] );
11 What is method overriding and mention its rules?
 If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
 In other words, If a subclass provides the specific implementation of the method that
has been declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
 Method overriding is used to provide the specific implementation of a method which
is already provided by its superclass.
 Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
 The method must have the same name as in the parent class
 The method must have the same parameter as in the parent class.
There must be an IS-A relationship (inheritance).
12 Do Interface differ from Abstract Class? – Justify
 Interface only defines method signatures; it cannot have any method implementation
(except static or default methods).
 Abstract Class can contain both abstract methods (without a body) and concrete
methods (with a body).

 Concluded as, Interfaces are used to specify a contract that multiple classes can
implement, while abstract classes provide a base with shared code that can be
inherited. In most languages, classes can implement multiple interfaces but only inherit
from one abstract class.
13 Illustrate why Creating an object of the sub class invokes also the constructor of
the super class.
When you create an object of a subclass in object-oriented programming, the constructor
of the superclass is invoked as well. This behavior ensures that the entire object is
properly initialized and that the base class's state and behavior are correctly set up before
the subclass-specific initialization occurs.
Example
// Superclass

5
class Animal {
// Constructor of the superclass
Animal() {
System.out.println("Animal constructor called");
}}
// Subclass
class Dog extends Animal {
// Constructor of the subclass
Dog() {
System.out.println("Dog constructor called");
}}

// Main class to test the constructors


public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
}}

Output:
Animal constructor called Dog constructor called
14 When a Class must be declared as Abstract?
 In Java, a class must be declared as abstract when it has an abstract method or
doesn't provide an implementation for abstract methods in its superclasses. Abstract
classes are also used when the child classes of the base class are closely related.
 Abstract classes can't be directly instantiated, which means an object can't be created
from them. This protects code from being used incorrectly.
 Abstract classes require subclasses to further define attributes necessary for
individual instantiation. When an abstract class is subclassed, the subclass usually
provides implementations for all of the abstract methods in its parent class. If it
doesn't, then the subclass must also be declared abstract.
15 Discover Extending an Interface with its syntax.
An interface contains variables and methods like a class but the methods in an interface
are abstract by default unlike a class. An interface extends another interface like a class
implements an interface in interface inheritance.
Example
interface A {
Void funcA();
}
interface B extends A {
void funcB();

6
}
class C implements B {
public void funcA() {
System.out.println("This is funcA");
}
public void funcB() {
System.out.println("This is funcB");
}}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
}}

Output
This is funcA
This is funcB
16 Define Runtime Polymorphism.
 Runtime polymorphism or Dynamic Method Dispatch is a process in which a call
to an overridden method is resolved at runtime rather than compile-time.
 In this process, an overridden method is called through the reference variable of
a superclass. The determination of the method to be called is based on the object being
referred to by the reference variable.
17 Brief Upcasting with an example.
Upcasting is a type of object typecasting in which a child object is typecasted to
a parent class object. By using the Upcasting, we can easily access the variables and
methods of the parent class to the child class. Here, we don't access all the variables and
the method. We access only some specified variables and methods of the child class.
Upcasting is also known as Generalization and Widening.

If the reference variable of Parent class refers to the object of Child class, it is known as
upcasting. For example:

class A{}
class B extends A{}

7
A a=new B();//upcasting

18 Multiple inheritance is not supported. Why?


The major reason behind Java’s lack of support for multiple inheritance lies in its design
philosophy of simplicity and clarity over complexity. By disallowing Multiple
Inheritance, Java aims to prevent the ambiguity and complexities that can arise from
having multiple parent classes.

The diamond problem is the classic issue that can arise with multiple inheritance. It
occurs when the class inherits from the two classes that have a common ancestor. This
situation forms the diamond-shaped inheritance hierarchy. It can lead to ambiguity in the
method resolution. Consider the scenario where class D inherits from classes B and C
which both inherits from Class A. This creates the diamond-shaped inheritance hierarchy.
In this scenario, both the classes B and C override the check() method inherited from the
class A. Now when the class D inherits from the B and C then the conflict arises because
class D doesn’t know which check() method to use.

19 Exemplify the uses of Super Keyword.


Usage of Java super Keyword
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor
20 Illustrate why Creating an object of the sub class invokes also the constructor of the
super class.
When you create an object of a subclass in object-oriented programming, the constructor
of the superclass is invoked as well. This behavior ensures that the entire object is
properly initialized and that the base class's state and behavior are correctly set up before
the subclass-specific initialization occurs.

8
Example
// Superclass
class Animal {
// Constructor of the superclass
Animal() {
System.out.println("Animal constructor called");
}}
// Subclass
class Dog extends Animal {
// Constructor of the subclass
Dog() {
System.out.println("Dog constructor called");
}}

// Main class to test the constructors


public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
}}

Output:
Animal constructor called Dog constructor called
Part B
1 Analyze the differences between Method Overloading and Method Overriding in Java in
terms of syntax, behavior, and execution time. Evaluate their use by providing code
examples.
2 Determine how to define an interface in Java application? Justify why interface members
are implicitly public, static, and final with example.
3 Elaborate how can we Overload the Constructor? Explain by using Multilevel Inheritance.
4 Discuss about the Abstract Class and Abstract Methods. Explain it by using Multilevel
Inheritance with example.
5 Can you give a detailed sketch of the differences between Single, Multilevel &
Hierarchical Inheritance?
6 Discuss how reusability of code can be achieved through inheritance with an example.
7 Explain about Super Keyword and how it can be used to access the superclass with an
example.
8 Describe about the Dynamic Method Dispatch using the Hierarchical Levels of
Inheritance with explanation.

9
9 Create an abstract Reservation class which has Reserve() abstract method. Implement the
sub-classes like ReserveTrain and ReserveBus classes and implement the same. Use
necessary variables, methods for reserving tickets.
10 Design a student management system using an abstract class Person and interfaces like
Graded and Registered. Implement classes like Undergraduate, Postgraduate, and PhD
Student that use multiple inheritance via interfaces.

10

You might also like