Java Classes Objects and Methods Interview Questions

Showing 1 - 10 of 51 results

How many ways you can create an Object in Java?

There are the five ways to create the object or get the object in java:

1. By using the new operator

Test t = new Test();

2.  By using the newInstance() Method

Test t = (Test)class.forName("Test").newInstance();

3. By using the factory Method

Runtime r = Runtime.getRuntime();
DateFormat d = Dateformat.getInstance();

4. By using the clone() Method

Test t = new Test();
Test t1= (Test)t.clone();

5. Bu using the deserialization

FileInputStream fis = new FileInputStream("Vinay");
ObjectInputStream obj = new ObjectInputStream(fis);
Dog d = (Dog)obj.readObject();

Not:- There is another way to create the object in Java but this is not standard, it is applicable only for the String

String s = "Vinay";

What will be the output of below Java code?

Example Code: 

public class Test
{
    int i =10;

    public static void main(String[] args)
    {
        System.out.println(i);
    }
}

Output: Error, Cannot make a static reference to the non-static field i.

What will be the output of below Java code?

Example Code: 

public class A
{
    int i = 999;
    System.out.println(i);
}

public class B extends A
{
    int i = 777;
    System.out.println(i);
}

public class C extends B
{
    int i = 888;
    System.out.println(i);
}

public class Test
{
    public void main(String args[])
    {
        C c = new C();
        c.i;               //Line :- 1
       ((B)c).i;        //Line :-2
       ((A)(B)c).i;   //Line :- 3
    }
}

Output

Line 1:- 888
Line 2:- 777
Line 3:- 999

What will be the output of below Java code?

Example Code: 

public class A
{
    public static void m1()
    {
        System.out.println("A")
    }
}

public class B extend A
{
    public static void m1()
    {
        System.out.println("B");
    }
}

public class C extends B
{
    public static void m1()
    {
        System.out.println("C");
    }
}

public class Test
{
    public void main(String args[])
    {
        C c = new C();
        c.m1();           //Line :- 1
       (B)c.m1();        //Line :-2
       (A)(B)c.m1();   //Line :- 3
    }
}

Output
Line 1:- C
Line 2:- B
Line 3:- A

Note:- Because in the method hiding concept, Method resolution is based on a compile-time object reference.

What is coupling in Java?

The degree of dependency between the components is called coupling. There are two types of coupling in java:-

1. Tight coupling: Degree of dependency between components is high that is called tight coupling. In general, Tight coupling means the two classes often change together. In other words, if A knows more than it should about the way in which B was implemented, then A and B are tightly coupled.

When an object creates the object to be used, then it is a tight coupling situation. As the main object creates the object itself, this object can not be changed from outside world easily marked it as tightly coupled objects.

Example:

class Subject 
{
    Topic t = new Topic(); 

    public void startReading()
    {
        t.understand();
    } 
} 

class Topic
{
    public void understand()
    {
        System.out.println("Tight coupling concept");
    }
}

In the above program the Subject class is dependents on Topic class. In the above program Subject class is tightly coupled with Topic class it means if any change in the Topic class requires Subject class to change. For example, if Topic class understand() method change to gotit() method then you have to change the startReading() method will call gotit() method instead of calling understand() method.

2. Loose coupling: Degree of dependency between components is low that is called loose coupling. Loose coupling is the best choice of coding.

When an object gets the object to be used from the outside, then it is a loose coupling situation. As the main object is merely using the object, this object can be changed from the outside world easily marked it as loosely coupled objects.

Example:

public interface Topic 
{
    void understand(); 
} 

class Topic1 implements Topic
{
    public void understand()
    {
        System.out.println("Got it");
    }
} 

class Topic2 implements Topic
{
    public void unserstand()
    {
        System.out.println("understand");
    } 
}

public class Subject
{
    public static void main(String[] args)
    {
        Topic t = new Topic1();
        t.understand();
    }
} 

In the above example, Topic1 and Topic2 objects are loosely coupled. It means Topic is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.

What is Polymorphism?

Polymorphism is briefly described as "one interface, many implementations". Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:

  • Compile time polymorphism
  • Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

Which one is the tightly encapsulated class in Java?

/*** e.g 1 ***/
public class A
{
    private int a;
}

public class B extends A
{
    private int s;
}

/*** e.g 2 ***/

public class C
{
    int a;
}

public class E extends C
{
    private  int s;
}

Ans: e.g 1

Key points about Tightly encapsulated class

  • A tightly encapsulated class does not allow direct public access to the internal data model. Instead, access is permitted only through accessor (i.e. get) and mutator (i.e. set) methods. The additional time required to work through the accessor and mutator methods typically slows execution speed.
  • A tightly encapsulated class does not allow public access to any data member that can be changed in any way. So encapsulation helps to protect internal data from the possibility of corruption from external influences.
  • The mutator methods can impose constraints on the argument values. If an argument falls outside of the acceptable range, then a mutator method could throw an IllegalArgumentException.
  • The internal design of a tightly encapsulated class can change while the public interface remains unchanged. An immutable class is always tightly encapsulated, but not every tightly encapsulation class is immutable.
  • Encapsulation enhances the maintainability of the code. A tightly encapsulated class allows access to data only through accessor and mutator methods.
  • A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model.
  • The data members of a tightly encapsulated class are declared private, so changes to the data model are less likely to impact external code.
  • Access to internal data can be provided by public accessor (i.e. get) and mutator (i.e. set) methods.

What is the encapsulation in Java ?

In object-oriented programming, encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components. Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.

  • Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
  • As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
  • Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.

For example:

public class A
{  
    private int i;
    
    public void setMethod(int i)
    {
        this.i = i;
    }
    
    public int getMethod()
    {
        return this.i;
    }
}

 

What is the need of an abstract class constructor in Java?

An abstract class constructor is used to initialize the instance variable. The biggest advantage of an abstract class constructor is the code reusability, it's means there is no need of initialize the instance variable of parent class inside the child class.

You can either explicitly provide constructor to abstract class or if you don't, compiler will add default constructor of no argument in abstract class. This is true for all classes and its also applies on abstract class.

Subscribe to Java Classes Objects and Methods Interview Questions