class and interface Based Interview Questions

1. What's the difference between an interface and an abstract class? 
An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

2. Can an inner class declared inside of a method access local variables of this method? 
Yes, it is possible if the variables are declared as final.

3. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces? 
Sometimes. But your class may be a descendent of another class and in this case the interface is your only option because Java does not support multiple inheritance.

4. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? 
You do not need to specify any access level, and Java will use a default package access level. A class with default access will be accessible only to other classes that are declared in the same directory/package.

5. When you declare a method as abstract method ? 
We declare a method as abstract, When we want child class to implement the behavior of the method.

6. Can I call a abstract method from a non abstract method ? 
Yes, We can call a abstract method from a Non abstract method in a Java abstract class

7. What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ? 
Abstract classes let you define some behavior while forcing your subclasses to provide the rest. These abstract classes will provide the basic funcationality of your application, child class which inherit this class will provide the funtionality of the abstract methods in abstract class.

Whereas, An Interface can only declare constants and instance methods, but cannot implement any default behavior.

If you want your class to extend some other class but at the same time re-use some features outlined in a parent class/interface - Interfaces are your only option because Java does not allow multiple inheritance and once you extend an abstract class, you cannot extend any other class. But, if you implement an interface, you are free to extend any other concrete class as per your wish.

Also, Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.

8. What are different types of inner classes ? 
Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes are of four types

1. Static member classes
2. Member classes
3. Local classes
4. Anonymous classes

9. What are the field/method access levels (specifiers) and class access levels ? 
Each field and method has an access level corresponding to it:

private: accessible only in this class
package: accessible only in this package
protected: accessible only in this package and in all subclasses of this class
public: accessible everywhere this class is available

Similarly, each class has one of two possible access levels:

package: class objects can only be declared and manipulated by code in this package
public: class objects can be declared and manipulated by code in any package

10. What modifiers may be used with an inner class that is a member of an outer class? 
A non-local inner class may be declared as public, protected, private, static, final, or abstract.

11. Can an anonymous class be declared as implementing an interface and extending a class? 
An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

12. What must a class do to implement an interface? 
It must provide implementation to all of the methods in the interface and identify the interface in its implements clause in the class declaration line of code.

13. What is the difference between a static and a non-static inner class? 
A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.

14. When can an object reference be cast to an interface reference? 
An object reference be cast to an interface reference when the object implements the referenced interface.

15. If a class is declared without any access modifiers, where may the class be accessed?
A class that is declared without any access modifiers is said to have default or package level access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

16. Which class should you use to obtain design information about an object? 
The Class class is used to obtain information about an object's design.

17. What modifiers may be used with an interface declaration? 
An interface may be declared as public or abstract.

18. Is a class a subclass of itself? 
Yes, a class is a subclass of itself.

19. What modifiers can be used with a local inner class?
A local inner class may be final or abstract.

20. Can an abstract class be final? 
An abstract class may not be declared as final. Abstract and Final are two keywords that carry totally opposite meanings and they cannot be used together.

21. What is the difference between a public and a non-public class?
A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

22. What modifiers may be used with a top-level class? 
A top-level class may be public, abstract, or final.

23. What are the Object and Class classes used for? 
The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

24. Can you make an instance of abstract class 
No you cannot create an instance of abstract class. If you use new keyword to instantiate an abstract class, you will get a compilation error.

25. Describe what happens when an object is created in Java 
Several things happen in a particular order to ensure the object is created properly:

1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the
object and its superclasses. Implemenation-specific data includes pointers to class and method data.

2. The instance variables of the objects are initialized to their default values.

3. The constructor for the most derived class is invoked. The first thing a constructor does is call the
consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called,
as java.lang.Object is the base class for all objects in java.

4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

26. What is the purpose of System Class 
The purpose of the system class is to provide the access to the System reources

27. What is instanceOf operator used for 
It is used to check if an object can be cast into a specific type without throwing Class cast exception

28. Why we should not have instance variable in an interface? 
Since all data fields and methods in an Interface are public by default, when we implement that interface in our class, we have public members in our class and this class will expose these data members and this is violation of encapsulation as now the data is not secure

29. What is a singleton class 
A singleton is an object that cannot be instantiated more than once. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy. We accomplish this by declaring the constructor private and having a public static instance variable of the class's type that can be accessed using a getInstance() method in the class.

30. Can an abstract class have final method 
Yes, you can have a final method in an Abstract class.

31. Can a final class have an abstract method 
No, a Final class cannot have an Abstract method.

32. When does the compiler insist that the class must be abstract 
The compiler insists that your class be made abstract under the following circumstances:

1. If one or more methods of the class are abstract.
2. If class inherits one or more abstract methods from the parent abstract class and no implementation is provided for that method
3. If class implements an interface and provides no implementation for some methods

33. How is abstract class different from final class 
Abstract class must be subclassed and an implementation has to be provided by the child class whereas final class cannot be subclassed

34. What is an inner class 
An inner class is same as any other class, just that, is declared inside some other class

35. How will you reference the inner class 
To reference an inner class you will have to use the following syntax: OuterClass$InnerClass

36. Can objects that are instances of inner class access the members of the outer class 
Yes they can access the members of the outer class

37. Can inner classes be static 
Yes inner classes can be static, but they cannot access the non static data of the outer classes, though they can access the static data.

38. Can an inner class be defined inside a method 
Yes it can be defined inside a method and it can access data of the enclosing methods or a formal parameter if it is final

39. What is an anonymous class 
Some classes defined inside a method do not need a name, such classes are called anonymous classes.

40. What are access modifiers 
These public, protected and private, these can be applied to class, variables, constructors and methods. But if you don't specify an access modifier then it is considered as Friendly. They determine the accessibility or visibility of the entities to which they are applied.

41. Can protected or friendly features be accessed from different packages 
No when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package.

42. How can you access protected features from another package 
You can access protected features from other classes by subclassing the that class in another package, but this cannot be done for friendly features.