0% found this document useful (0 votes)
8 views4 pages

Java Inheritance & Exception Handling Q&A

The document covers Java concepts related to inheritance, abstract classes, interfaces, and exception handling. It explains inheritance types, constructor chaining, method overriding, and the differences between abstract classes and interfaces. Additionally, it discusses exception handling mechanisms, including try-catch blocks, checked vs unchecked exceptions, and custom exceptions.

Uploaded by

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

Java Inheritance & Exception Handling Q&A

The document covers Java concepts related to inheritance, abstract classes, interfaces, and exception handling. It explains inheritance types, constructor chaining, method overriding, and the differences between abstract classes and interfaces. Additionally, it discusses exception handling mechanisms, including try-catch blocks, checked vs unchecked exceptions, and custom exceptions.

Uploaded by

Rishit Jain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Theory Q&A – Unit 4 (Inheritance,

Abstract Classes, Interfaces) & Unit 5


(Exception Handling)
Unit 4 – Inheritance, Abstract Classes, Interfaces
 Q: What is inheritance in Java? Explain its types with examples.

A: Inheritance allows a class (subclass) to inherit properties and methods from another
class (superclass). It promotes code reuse.
Types:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid (via interfaces)
- Multiple Inheritance (via interfaces)
Example: class Dog extends Animal

 Q: How is constructor chaining handled in inheritance?

A: Constructor chaining is done using super(). The subclass constructor calls the superclass
constructor using super().

 Q: What is the order of constructor execution in multilevel inheritance?

A: Constructors are executed from top to bottom. Superclass constructor runs before
subclass.

 Q: What is the use of the super keyword? Explain with examples.

A: super is used to access parent class methods/variables and call parent constructor from
child class.

 Q: Can constructors be inherited? Why or why not?

A: No. Constructors are not inherited but can be called using super().

 Q: What is method overriding? How does it support runtime polymorphism?


A: Overriding occurs when a subclass provides a specific implementation of a method
already present in the superclass. At runtime, Java uses object type to resolve the method.

 Q: What is the difference between method overloading and overriding?

A: Overloading: same method name, different parameters.


Overriding: same method name and parameters in subclass.
Binding: Overloading = compile-time; Overriding = runtime.

 Q: What is dynamic method dispatch in Java? Explain with example.

A: When a superclass reference points to a subclass object, and overridden methods are
resolved at runtime. Enables polymorphism.

 Q: What is binding? Differentiate between static and dynamic binding.

A: Binding links method calls to code. Static = compile-time (e.g., overloading), Dynamic =
runtime (e.g., overriding).

 Q: What is the difference between an abstract class and an interface?

A: Abstract class: partial abstraction, can have constructors.


Interface: full abstraction (till Java 7), supports multiple inheritance.

 Q: Can a class implement multiple interfaces? Explain with an example.

A: Yes. A class can implement multiple interfaces separated by commas.

 Q: Can an interface extend another interface? If yes, how?

A: Yes, using extends. Interface B extends A.

 Q: What are the similarities and differences between abstract classes and interfaces?

A: Similarities: can't be instantiated, used for abstraction.


Differences: Abstract classes can have constructors, interfaces can't.

 Q: When should you prefer interfaces over abstract classes?

A: Use interfaces for full abstraction or when you need multiple inheritance.
 Q: Can interfaces have constructors or static blocks?

A: No. Interfaces cannot have constructors or static blocks.

 Q: What are the access modifiers in Java? Explain each with examples.

A: private (class only), default (package), protected (subclasses), public (everywhere).

 Q: What is the significance of the final keyword in Java?

A: final variable = constant, final method = can't override, final class = can't inherit.

 Q: What is the purpose of the this keyword? How is it different from super?

A: this = current class instance. super = parent class instance.

Unit 5 – Exception Handling (Without Multithreading)


 Q: What is an exception in Java? Explain its types.

A: An exception is a runtime error. Types: Checked (IOException), Unchecked


(ArithmeticException).

 Q: What is the difference between checked and unchecked exceptions?

A: Checked: must be declared/handled. Unchecked: don't need to declare.


Examples: Checked = IOException, Unchecked = NullPointerException

 Q: What is the purpose of try, catch, and finally blocks?

A: try: risky code, catch: handle exceptions, finally: always runs (for cleanup).

 Q: What is the difference between throw and throws?

A: throw: used to throw exception object. throws: used to declare exceptions in method
signature.

 Q: Can a method declare one exception using throws but still throw others?

A: Yes. Other unchecked exceptions can still occur and don't need to be declared.
 Q: What is a custom (user-defined) exception? How is it created and used?

A: Create a class extending Exception. Throw it using throw new CustomException().

 Q: What happens if an exception is thrown but not caught?

A: The program crashes and the JVM prints a stack trace.

 Q: Will the finally block execute if the exception is not caught?

A: Yes. Finally block always executes unless the JVM exits forcibly.

 Q: What are multiple catch blocks and when are they useful?

A: When different exceptions can occur and need to be handled differently.

 Q: What is the use of the finalize() method? When is it called?

A: Called by garbage collector before object is destroyed. Deprecated in recent Java versions.

Common questions

Powered by AI

A custom exception in Java is defined by creating a class that extends 'Exception' or 'RuntimeException'. For example, class MyException extends Exception. This is advantageous in scenarios where specific application constraints or rules need to be enforced, providing clarity and maintainability by encapsulating error conditions in well-defined exception types .

The 'finally' block is used in exception handling to execute code after a try-catch block and is guaranteed to execute regardless of whether an exception was caught, ensuring cleanup code runs. It will execute except in cases like JVM crashes or System.exit() calls. Its main role is to ensure that resources are freed or other cleanup operations are performed, enhancing robustness .

Method binding in Java refers to the linking of method calls with the method body. Static binding occurs at compile time where method calls and fields are resolved using the reference type (e.g., method overloading). Dynamic binding, or late binding, occurs at runtime where overridden methods are resolved using the actual object's type (e.g., method overriding), supporting polymorphism .

Both abstract classes and interfaces cannot be instantiated and are used to define contract-like structures to achieve abstraction. Abstract classes can have constructors, state, and provide default behavior, supporting partial abstraction. Interfaces, until Java 7, supported full abstraction and facilitated multiple inheritance. While abstract classes suit scenarios where shared state or behavior among subclasses is needed, interfaces are ideal for defining capabilities across unrelated classes .

The 'super' keyword is used in a subclass to call methods and variables of its superclass or to invoke a parent constructor. It is significant in method overriding as it allows the subclass to call the overridden superclass method when needed, thus providing access to perform additional functionality on top of the superclass method .

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at runtime, allowing Java to support runtime polymorphism. It involves a superclass reference pointing to a subclass object, and the appropriate overridden method is invoked at runtime based on the actual object type, not the reference type, enabling dynamic behavior .

Constructor chaining in Java is achieved using the 'super()' keyword, which ensures that the constructor for the parent class is called before the child class constructor. This is crucial in multilevel inheritance where the constructors are executed in a hierarchical order from the superclass down to the subclass, thereby ensuring that all the necessary initialization steps defined in the hierarchy are executed properly .

Interfaces are preferred when full abstraction is needed or when you need to achieve multiple inheritance, which is not possible with abstract classes. This is because a class can implement multiple interfaces but can only extend one abstract class. Interfaces provide a way to define a contract for behaviors without concerning about implementation details, thus promoting loose coupling and high cohesion .

Java does not support multiple inheritance with classes to avoid complexity and ambiguity (e.g., the Diamond Problem). Interfaces however provide a means to achieve multiple inheritance because they allow a class to implement multiple interfaces. This enables a class to inherit method signatures from multiple sources while maintaining clear and conflict-free contract definitions, thus offering flexibility without the downsides of multiple class inheritance .

Checked exceptions must be either declared in the method signature using 'throws' or be handled within a try-catch block, as they are checked at compile time. Examples include IOException. Unchecked exceptions, on the other hand, are not required to be declared or handled explicitly as they occur due to programming errors like NullPointerException; they are checked at runtime .

You might also like