Java Interview Questions

Showing 1 - 10 of 199 results

Are inheritance and overriding concept applicable for the constructor in Java?

In java, inheritance and the overriding concept are not applicable for constructors. In other words, if we inherit a class then the constructor of the parent class is not available in child class.

Constructor Overriding is never possible in Java. This is because, Constructor looks like a method but name should be as class name and no return value. Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding.

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 will be the output of below Java code?

Example Code: 

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

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

public class C extends B
{
    public 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: C
Line 3: C

What are the rules for the Type Casting in Java?

Type casting is when you assign a value of one primitive data type to another type.

B b = new B();
A a = (A)b;

Rules:

  • There must be some relation between A and B, it can be parent and child relationship.
  • Type of (A)b must be the A or derived object of A.

Example

String s = new String("Durga");
Object o = (Object)s;

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.

Subscribe to Java Interview Questions