#2 Random Questions for practice on Basic Java Programing
#2 Random Questions for practice on Basic Java Programing
Q #11)
What is Encapsulation?
Answer:
Purpose of Encapsulation:
·
Protects the code from others.
·
Code maintainability.
·
Wrapping of details
·
Can be achive by getter and setter
Example:
We are declaring ‘a’ as
an integer variable and it should not be negative.
|
public class Addition(){ int a=5; } |
If
someone changes the exact variable as “a = -5” then it is
bad.
In
order to overcome the problem we need to follow the steps below:
·
We can make the variable private or protected.
·
Use public accessor methods such as set<property> and
get<property>.
So that
the above code can be modified as:
|
public class Addition(){ private int a = 5; //Here the variable
is marked as private } |
The
code below shows the getter and setter.
Conditions can be
provided while setting the variable.
|
get A(){ } set A(int a){ if(a>0){// Here
condition is applied } } |
For encapsulation, we
need to make all the instance variables private and create setter and getter
for those variables. Which in turn will force others to call the setters rather
than access the data directly.
Q #12)
What is Polymorphism?
Answer: Polymorphism
means many forms.
A single object can
refer to the super-class or sub-class depending on the reference type which is
called polymorphism.
Example:
|
Public class Manipulation(){ //Super
class public void add(){ } } public class Addition extends Manipulation(){ // Sub
class public void add(){ } public static void main(String args[]){ Manipulation addition =
new
Addition();//Manipulation
is reference type and Addition is reference type addition.add(); } } |
Using
the Manipulation reference type we can call the Addition class “add()” method.
This ability is known as Polymorphism. Polymorphism is applicable for overriding and
not for overloading.
Q #13)
What is meant by Method Overriding?
Answer:
Method overriding happens if the sub-class method satisfies the below
conditions with the Super-class method:
·
Method name should be the same
·
The argument should be the same
·
Return type should also be the same
The key benefit of
overriding is that the Sub-class can provide some specific information about
that sub-class type than the super-class.
Example:
|
public class Manipulation{ //Super class public void add(){ ……………… } } Public class Addition extends Manipulation(){ Public void add(){ ……….. } Public static void main(String args[]){ Manipulation addition =
new
Addition();
//Polimorphism is applied addition.add(); // It
calls the Sub class add() method } } |
addition.add() method
calls the add() method in the Sub-class and not the parent class. So it
overrides the Super-class method and is known as Method Overriding.
Q #14)
What is meant by Overloading?
Answer: Method
overloading happens for different classes or within the same class.
For
method overloading, sub-class method should satisfy the below conditions with
the Super-class method (or) methods in the same class itself:
·
Same method name
·
Different argument types
·
There may be different return types
Example:
|
public class Manipulation{ //Super
class public void add(String name){ //String
parameter ……………… } } Public class Addition extends Manipulation(){ Public void add(){//No Parameter ……….. } Public void add(int a){ //integer parameter } Public static void main(String args[]){ Addition addition = new Addition(); addition.add(); } } |
Here the add() method
has different parameters in the Addition class is overloaded in the same class
as with the super-class.
Note: Polymorphism
is not applicable for method overloading.
Q #15)
What is meant by Interface?
Answer: Multiple
inheritances cannot be achieved in java. To overcome this problem the Interface
concept is introduced.
An interface is a
template which has only method declarations and not the method implementation.
Example:
|
Public abstract interface IManupulation{ //Interface
declaration Public abstract void add();//method declaration public abstract void subtract(); } |
·
All the methods in the interface are internally public
abstract void.
·
All the variables in the interface are internally public
static final that is constants.
·
Classes can implement the interface and not extends.
·
The class which implements the interface should provide an
implementation for all the methods declared in the interface.
|
public class Manupulation implements IManupulation{
//Manupulation class uses the interface Public void add(){ …………… } Public void subtract(){ ……………. } } |
Q #16)
What is meant by Abstract class?
Answer: We
can create the Abstract class by using the “Abstract” keyword before the class
name. An abstract class can have both “Abstract” methods and “Non-abstract”
methods that are a concrete class.
Abstract
method:
The method which has
only the declaration and not the implementation is called the abstract method
and it has the keyword called “abstract”. Declarations ends with a semicolon.
Example:
|
public abstract class Manupulation{ public abstract void add();//Abstract method
declaration Public void subtract(){ } } |
·
An abstract class may have a non- abstract method also.
·
The concrete Subclass which extends the Abstract class should
provide the implementation for abstract methods.
Comments
Post a Comment