Q)Explain about a final class.
=>Once a class is made final, inheritance is prevented.I.e. sub classes can’t be created from a final class.
For eg.
final class A{
}
class B extends A{
}//ERROR.
=>Generally, the most specialized class in the hierarchy is made final.
=>To make a class immutable(non modifiable) also, that class can be made final.
For eg. String class.
abstract modifier
-------------------
=>"abstract" is a keyword in Java.
=>"abstract" keyword can be applied to classes and methods but not to variables.
Q)Explain about an abstract method.
=>If "abstract" modifier is applied to a method, it becomes an abstract method.
=>An abstract method can’t have body.I.e. an abstract method has declaration but
no definition.
For eg. abstract void move();
=>an abstract method in Java is similar to function prototype in C.
=>If a method is declared abstract, its class also must be declared abstract.I.e. a non abstract class can’t
contain abstract methods.
class Vehicle{
abstract void move();
}//ERROR.
=>In the above example, Vehicle class is not declared abstract.Therefore, compilation error comes.
Q)What is wrong with the following code?
abstract class Vehicle{
abstract void move(){
}
}
Ans:- An abstract method can’t have body.
Q)What is the purpose of abstract methods?
=>To enforce method overriding in the sub classes, abstract methods are declared in a super class.
=>Once a method is declared abstract in a class, all its sub classes must implement/override that method.
Q)What is wrong with the following code?
=>Once a class is made final, inheritance is prevented.I.e. sub classes can’t be created from a final class.
For eg.
final class A{
}
class B extends A{
}//ERROR.
=>Generally, the most specialized class in the hierarchy is made final.
=>To make a class immutable(non modifiable) also, that class can be made final.
For eg. String class.
abstract modifier
-------------------
=>"abstract" is a keyword in Java.
=>"abstract" keyword can be applied to classes and methods but not to variables.
Q)Explain about an abstract method.
=>If "abstract" modifier is applied to a method, it becomes an abstract method.
=>An abstract method can’t have body.I.e. an abstract method has declaration but
no definition.
For eg. abstract void move();
=>an abstract method in Java is similar to function prototype in C.
=>If a method is declared abstract, its class also must be declared abstract.I.e. a non abstract class can’t
contain abstract methods.
class Vehicle{
abstract void move();
}//ERROR.
=>In the above example, Vehicle class is not declared abstract.Therefore, compilation error comes.
Q)What is wrong with the following code?
abstract class Vehicle{
abstract void move(){
}
}
Ans:- An abstract method can’t have body.
Q)What is the purpose of abstract methods?
=>To enforce method overriding in the sub classes, abstract methods are declared in a super class.
=>Once a method is declared abstract in a class, all its sub classes must implement/override that method.
Q)What is wrong with the following code?