JAVA important question-Answer
JAVA important question-Answer
2.What are the roles of abstract class & abstract method in java.
Ans:
An abstract class is a class that cannot be directly used to create objects. It is
meant to be inherited by other classes. It can have both abstract methods
(without body) and normal methods (with body).
Role: 1. Used to provide a base structure for other classes.
2.Helps in code reuse and defining common behaviour.
16.Explain how do work abstract method and how it is different in normal method
in java programming.
Ans: An abstract method is a method without a body (no implementation). It is
declared with the keyword abstract. It must be defined inside an abstract class.
Subclasses must override it to provide implementation.
Syntax: abstract void show(); // no body
A normal method has a body (implementation). It can be in any class (abstract or
regular). Can be used directly or overridden optionally.
Difference:
Aspect Abstract Method Normal Method
Body No body Has a body
Must use abstract No need for abstract
Declaration
keyword keyword
Inside Class Only in abstract classes In any class
Must be overridden in Can be used directly or
Usage
subclass overridden