Oops based Java Interview Questions and Answers

Class and Object : constructor, local and instance variable, 

What do you mean by Constructor?
Ans: The points given below explain what a Constructor is in detail:
  • When a new object is created in a program a constructor gets invoked corresponding to the class.
  • The constructor is a method which has the same name as class name.
  • If a user doesn’t create a constructor implicitly a default constructor will be created.
  • The constructor can be overloaded.
  • If the user created a constructor with a parameter then he should create another constructor explicitly without a parameter.
What is meant by Local variable and Instance variable?
Ans: Local variables are defined in the method and scope of the variables that have existed inside the method itself.
An instance variable is defined inside the class and outside the method and scope of the variables exist throughout the class.
What is a Class?
Ans: All Java codes are defined in a class. A Class has variables and methods.
Variables are attributes which define the state of a class.
Methods are the place where the exact business logic has to be done. It contains a set of statements (or) instructions to satisfy the particular requirement.
Example:
1public class Addition{ //Class name declaration
2int a = 5//Variable declaration
3int b= 5;
4public void add(){ //Method declaration
5int c = a+b;
6}
7}
What is an Object?
Ans: An instance of a class is called object. The object has state and behavior.
Whenever the JVM reads the “new()” keyword then it will create an instance of that class.
Example:
1public class Addition{
2public static void main(String[] args){
3Addion add = new Addition();//Object creation
4}
5}

The above code creates the object for the Addition class.

Encapsulation : Package, Access Modifier 

Explain about Public and Private access specifiers.
Ans: Methods and instance variables are known as members.
Public:
Public members are visible in the same package as well as the outside package that is for other packages.
Public
Public members in Class A are visible to Class B (Same package) as well as Class C (Different package).
Private:
Private members are visible in the same class only and not for the other classes in the same package as well as classes in the outside packages.
Private
Private members in class A is visible only in that class. It is invisible for class  B as well as class C.
Q #20) Difference between Default and Protected access specifiers.
Ans: Default: Methods and variables declared in a class without any access specifiers are called default.
Default
Default members in Class A are visible to the other classes which are inside the package and invisible to the classes which are outside the package.
So Class A members are visible to the Class B and invisible to the Class C.
Protected:
Protected             .
Protected is same as Default but if a class extends then it is visible even if it is outside the package.
Class A members are visible to Class B because it is inside the package. For Class C it is invisible but if Class C extends Class A then the members are visible to the Class C even if it is outside the package.


Inheritance



Polymorphism

What is Polymorphism?
Ans: Polymorphism means many forms.
A single object can refer the super class or sub-class depending on the reference type which is called polymorphism.
Example:
1Public class Manipulation(){ //Super class
2public void add(){
3}
4}
5public class Addition extends Manipulation(){ // Sub class
6public void add(){
7}
8public static void main(String args[]){
9Manipulation addition = new Addition();//Manipulation is reference type and Addition is reference type
10addition.add();
11}
12}
Using 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.
What is meant by Method Overriding?
Ans: Method overriding happens if the sub class method satisfies the below conditions with the Super class method:
  • Method name should be same
  • Argument should be same
  • Return type also should be 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.
What is meant by Overloading?
Ans: Method overloading happens for different classes or within the same class.
For method overloading, subclass method should satisfy the below conditions with the Super class method (or) methods in the same class itself:
  • Same method name
  • Different argument type
  • May have 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 having different parameters in the Addition class is overloaded in the same class as well as with the super class.

Abstraction

What is meant by Abstract class?
Ans: We can create the Abstract class by using “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 are the ends with a semicolon.
Example:
1public abstract class Manupulation{
2public abstract void add();//Abstract method declaration
3Public void subtract(){
4}
5}
  • 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.
Others


Comments

Popular posts from this blog

Jetpack Compose based Android interview and questions

Null safety based Kotlin interview questions and answers

CustomView based Android interview questions and answers