Java OOPS Interview Questions and Answers

    52 Votes

21. What’s an interface ?
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types.
There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
public interface Person {
    // constant declarations, if any
    String name;
    public String getName();
    public void setName(String iName);
}

 

22. How can we implement an interface in java ?

To use an interface, you write a class that implements the interface. When class implements an interface, it provides a method body for each of the methods declared in the interface

23. What is an Abstract class ?

Abstract classes in Java are classes which cannot be instantiated, meaning you cannot create new instances of an abstract class. The purpose of an abstract class is to function as a base for subclasses. You can declare that a class is abstract by adding the abstract keyword to the class declaration. Here is an example:
public abstract class MyAbstractClass {
}
24. What are Abstract methods ?

An abstract class can have abstract methods. You declare a method abstract by adding the abstract keyword in front of the method declaration. Abstract method do not have implementation. Abstract method should be implemented in the sub class which inherit them.
public abstract class MyAbstractClass {
    public abstract void abstractMethod();
} 

 

25. What’s the difference between 'Abstract' classes and 'Interfaces' ?
  • Abstract Class can only be inherited, while interface cannot be. Interface has to be implemented.
  • Abstract class can have implementation ,where as interface cannot implement methods.

26. What’s difference between Static and Non-Static fields of a class ?

Non static values are also called instance variables. Each object of class will be having its own copy of non static instance variable. Static variable will be having only one copy of instance variable which is shared among the objects of the class.
public class userLogin {
    // constant declarations, if any
    public int userid; // Static variable declaration
    public static int userCount; // Non Static variable declaration
}
27. What are inner classes ?

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
class OuterClass {
    ...
    class NestedClass {
        ...
    }
}

Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.

28. Why we are using inner classes ?

There are several compelling reasons for using nested classes. It is a way of logically grouping classes that are only used in one place. It increases encapsulation. Nested classes can lead to more readable and maintainable code.

29. What are packages ?

Package groups related classes and interface together and thus avoiding any name conflicts. Classes are grouped together in a package using the keyword 'package'
package administration;
public class userInfo {
    }
public class departmentInfo {
    }
30. What is a constructor in class ?

A constructor is a special method that is used to initialize a newly created object. The name of the constructor will be same as the class name. Constructor don't have a return type.
public class userInfo {
    int userCount;
    userInfo(){
        userCount=0;
    }
    userInfo(int count){
        userCount=count;
    }
}
Page 3 of 3

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java Interview Questions
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers