Skip to main content

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);
   }
}



Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...