Java OOPS interview questions and answers || Class and Object in Java.

Java OOPS interview questions and answers Like what is Class and Object in Java, a method in Java. As you know Java is an object-oriented programming language. But I have one question to you, Is Java is a pure object-oriented programming language? So sometimes my answer is yes and at the same time, it’s no. We will discuss it later or you can read developers review on that question Read, So let’s start a brief discussion on java Oops concept and Java OOPS interview questions and answers with the example.

Java makes the task easy to support oops concepts like.

  • Object and Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Object and Class in java.

An object is a runtime entity which has a behavior known as Object in java. In the below code new Main(); is an object.

A class is a collection of objects. which contains Methods, Constructor, Data Member. In the below code Main is a class.

Class Main{
public static void main(String args[]){

Main m = new Main();
m.message();

}
public void message(){

System.out.println("Hello Java");
}
}

 

Java oops interview questions and answers(Class and Object).

Q>What is class and Its type in java.

A Class is a blueprint or prototype that defines the variables and the methods, constructor, Data members. Mainly two types of Java classes are there some classes are prebuilt in java and we can also create our own classes in java.

Q> What are the methods and its advantage.

Methods are used to define the behavior of an object in java.

The advantage of the method in Java :

  • Code Reusability.
  • Code Optimization.

Q>Can we define a top level class as a private in java.

No, you can’t. A top-level class as private would be completely useless because nothing would have access to it. So we can define a top-level class as a public or default to access it.

Q>What is the reason behind this error “Could not found or load main class”?

When you get the error message “Could not find or load main class “, that means that the java command was not able to find the class.

Q>What are the different ways to create an object in java.

There are five different ways to create an object in Java

1. Using the new keyword:

Employee emp1 = new Employee();

2. Using newInstance() method of Class:

Employee emp2 = (Employee) Class.forName("class name").newInstance();

It can also be written as

Employee emp2 = Employee.class.newInstance();

  • By clone() method.
  • By deserialization.
  • By factory method etc.

Q>What are the different ways to initialize an object in java.

There are 3 ways to initialize an object in java.

  • By reference variable
  • By method
  • By constructor

Q> What are the inner classes in java.

A class inside other class is called inner class or nested class let’s see the example.

public class Main{
   class M{}}

Q> How can be used multiple objects of a class in java.

public class Main{
public static void main(String args[]){
    Class2 obj = new Class2();
    Class2 obj1 = new Class2();
    Class2 obj2 = new Class2();
    
    obj.show();
    obj1.show();
    obj2.show();
  }
}
class Class2{
  void show(){
    System.out.println("hello java");
  }
  
}

 

Java oops interview questions

Best collection of Java interview questions and answers.

 

 

 

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

5 × one =