Java Unit-2 Assignment Answers
Java Unit-2 Assignment Answers
We can not override the constructor as the parent and child class
can never have a constructor with the same name(The
constructor name must always be the same as the Class name).
5. Short note: abstract class and abstract method.
A class which is declared as abstract is known as an abstract
class. It can have abstract and non-abstract methods. It needs
to be extended and its method implemented. It cannot be
instantiated.
Rules for Abstract Class :
An abstract class must be declared with an abstract
keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not
to change the body of the method.
A method which is declared as abstract and does not have
implementation is known as an abstract method.
Example of abstract method :
abstract void printStatus(); //no method body and abstract
6. What is interface? Explain in detail.
In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
There are mainly three reasons to use interface. They are given
below.
1) Abstract class can have abstract Interface can have only abstract methods.
and non-abstract methods. Since Java 8, it can have default and static
methods also.
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.
8) A Java abstract class can have Members of a Java interface are public by
class members like private, protected, default.
etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }