JAVA Unit-2 Assignment Answers
1. What is an inheritance? Explain types of inheritance.
Inheritance is a mechanism of driving a new class from an
existing class. The existing (old) class is known as base
class or super class or parent class. The new class is known
as a derived class or sub class or child class. It allows us to
use the properties and behavior of one class (parent) in
another class (child).
Single Inheritance :
In single inheritance, a sub-class is derived from only one
super class. It inherits the properties and behavior of a
single-parent class.
Multi-level Inheritance :
a class is derived from a class which is also derived from
another class is called multi-level inheritance.
Hierarchical Inheritance :
If a number of classes are derived from a single base class,
it is called hierarchical inheritance.
Hybrid Inheritance :
Hybrid means consist of more than one. Hybrid inheritance
is the combination of two or more types of inheritance.
2. Short note: super keyword.
The super keyword refers to superclass (parent)
objects.
It is used to call superclass methods, and to access the
superclass constructor.
The most common use of the super keyword is to
eliminate the confusion between superclasses and
subclasses that have methods with the same name.
The super keyword in Java provides many advantages
in object-oriented programming are as follows:
Enables reuse of code
Supports polymorphism
Provides access to parent class behaviour
Allows for customization of behaviour
Facilitates abstraction and encapsulation
3. Short note: final keyword.
The final keyword is a non-access modifier used for
classes, attributes and methods, which makes them
non-changeable (impossible to inherit or override).
The final keyword is useful when you want a variable
to always store the same value, like PI (3.14159...).
Here are some of its characteristics:
Final variables: When a variable is declared as
final, its value cannot be changed once it has been
initialized.
Final methods: When a method is declared as
final, it cannot be overridden by a subclass.
Final classes: When a class is declared as final, it
cannot be extended by a subclass.
Initialization: Final variables must be initialized
either at the time of declaration or in the
constructor of the class.
Performance: The use of final can sometimes
improve performance, as the compiler can
optimize the code more effectively when it knows
that a variable or method cannot be changed.
Security: final can help improve security by
preventing malicious code from modifying
sensitive data or behavior.
4. Short note: method overriding.
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.
Rules for Java Method Overriding
1. Overriding and Access Modifiers
2. Final methods can not be overridden
3. Static methods can not be overridden
4. Private methods can not be overridden
5. The overriding method must have the same return type (or
subtype)
Overriding and Constructor
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.
An interface in Java is a blueprint of a class. It has static
constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There
can be only abstract methods in the Java interface, not method
body. It is used to achieve abstraction and multiple inheritance
in Java.
In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship.
It cannot be instantiated just like the abstract class.
There are mainly three reasons to use interface. They are given
below.
1. It is used to achieve abstraction.
2. By interface, we can support the functionality of multiple
inheritance.
3. It can be used to achieve loose coupling.
7. Difference between abstract class and interface.
Abstract class Interface
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.
2) Abstract class doesn't support Interface supports multiple inheritance.
multiple inheritance.
3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.
4) Abstract class can provide the Interface can't provide the
implementation of interface. implementation of abstract class.
5) The abstract keyword is used to The interface keyword is used to declare
declare abstract class. interface.
6) An abstract class can extend An interface can extend another Java
another Java class and implement interface only.
multiple Java interfaces.
7) An abstract class can be extended An interface can be implemented using
using keyword "extends". keyword "implements".
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();
} }