Friday 5 April 2019

Top 10 java OOPS interview questions


1) What is an object ? 

An Object is instance of class. A class defines type of object. Each object belongs to some class.Every object contains state and behavior. State is determined by value of attributes and behavior is called method.

Objects are alos called as an instance.

Lets consider this class :
public class FirstClass {
 public static void main(String[] args) {
  FirstClass f = new FirstClass();
  System.out.println("My First class");
 }
}

To instantiate the FirstClass we use this statement

object Initialize:
FirstClass f=new FirstClass(); 

f is used to refer FirstClass object.


2) How to call one constructor from the other constructor ? 

With in the same class if we want to call one constructor from other we use this() method. Based on the number of parameters we pass appropriate this() method is called.

Restrictions for using this method : 

1) this must be the first statement in the constructor
2)we cannot use two this() methods in the constructor



3) What is method overriding in java ? 

If we have methods with same signature (same name, same signature, same return type) in super class and subclass then we say subclass method is overridden by superclass.

When to use overriding in java If we want same method with different behaviour in superclass and subclass then we go for overriding.

When we call overridden method with subclass reference subclass method is called hiding the superclass method.


4) What is method overloading in java ? 

A class having two or more methods with same name but with different arguments then we say that those methods are overloaded.

Static polymorphism is achieved in java using method overloading.

Method overloading is used when we want the methods to perform similar tasks but with different inputs or values.

When an overloaded method is invoked java first checks the method name, and the number of arguments ,type of arguments; based on this compiler executes this method.

Compiler decides which method to call at compile time. By using overloading static polymorphism or static binding can be achieved in java.

Note : Return type is not part of method signature. we may have methods with different return types but return type alone is not sufficient to call a method in java.


5) Difference between this() and super() in java ?

this() is used to access one constructor from another with in the same class while super() is used to access superclass constructor.

Either this() or super() exists it must be the first statement in the constructor.



6) Why main() method is public, static and void in java ?

public : “public” is an access specifier which can be used outside the class. When main method is declared public it means it can be used outside class.

static : To call a method we require object. Sometimes it may be required to call a method without the help of object. Then we declare that method as static. JVM calls the main() method without creating object by declaring keyword static.

void : void return type is used when a method does’nt return any value .

main() method does’nt return any value, so main() is declared as void.


Signature : 

public static void main(String[] args) {


7) What is encapsulation ?

The process of wrapping or putting up of data in to a single unit class and
keeps data safe from misuse is called encapsulation .

Through encapsulation we can hide and protect the data stored in java objects.Java supports
encapsulation through access control.

There are four access control modifiers in java public , private ,protected and default level.

For example take a car class , In car we have many parts which is not required for driver to know what all it consists inside. He is required to know only about how to start and stop the car. So we can expose what all are required and hide the rest by using encapsulation.


8)What is constructor in java ?

A constructor is a special method used to initialize objects in java.

we use constructors to initialize all variables in the class when an object is created. As and when an object is created it is initialized automatically with the help of constructor in java.

We have two types of constructors

Default Constructor
Signature :
public classname()
{

}

Parameterized Constructor
Signature : 
public classname(parameters list)
{

}



9) What is ‘IS-A ‘ relationship in java?

‘is a’ relationship is also known as inheritance. We can implement ‘is a’ relationship or inheritance in java using extends keyword. The advantage or inheritance or is a relationship is reusability of code instead of duplicating the code.

Ex : Motor cycle is a vehicle
public class MotorCycle
{

}

Car is a vehicle

public class Car
{

}

Both car and motorcycle extends vehicle.
public class Car extends Vehicle
{

}

public class MotorCycle extends Vehicle
{

}


10) What is ‘HAS A’’ relationship in java?

‘Has a ‘ relationship is also known as “composition or Aggregation”. As in inheritance we have ‘extends’ keyword we don’t have any keyword to implement ‘Has a’ relationship in java.

The main advantage of ‘Has-A‘ relationship in java code reusability.

By this reference of class B, A can access all properties of class B which are allowed.

class HardDisk {
   public void writeData(String data) {
      System.out.println("Data is being written : " + data);
   }
}

class UseDell {
   // segate is referece of HardDisk class in UseDell class.
   // So, UseDell Has-A HardDisk
   HardDisk segate = new HardDisk();
   public void save (String data) {
      segate.writeData(data);
   }
}



No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...