OOPS Concepts


1) What are the OOPs principles and define them?

Encapsulation, Inheritance and Polymorphism are the three OOPs Principles. Encapsulation: Is the Mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Inheritance: Deriving an object from an existing class. In the other words, Inheritance is the process of inheriting all the features from a class Polymorphism: Is a feature that allows one interface to be used for a general class of actions.

2) What is the difference between superclass & subclass?

A super class is a class that is inherited whereas subclass is a class that does the inheriting.

3) Which keyword is used to inherit a class?

extends

4) Which class does begin Java class hierarchy?

Object class Object class is a superclass of all other classes.

5) What are the advantages of inheritance?

Reusability of code and accessibility of variables and methods of the superclass by subclasses.

6) Which method is used to call the constructors of the superclass from the subclass?

super(argument)

7) What are abstract classes?

Abstract classes are those for which instances can’t be created.

8) What must a class do to implement an interface?

It must provide all of the methods in the interface and identify the interface in its implements clause.

9) Which methods in the Object class are declared as final?

getClass(), notify(), notifyAll(), and wait()

10) Can we declare variable inside a method as final variables? Why?

Cannot because, local variable cannot be declared as final variables.

11) Can an abstract class may be final?

An abstract class may not be declared as final.

12) Does a class inherit the constructors of it's super class?

A class does not inherit constructors from any of it's super classes.

13) What restrictions are placed on method overloading?

Two methods may not have the same name and argument list but different return types.

14) What restrictions are placed on method overriding?

Overridden methods must have the same name , argument list , and return type. The overriding method may not limit the access of the method it overridees.The overriding method may not throw any exceptions that may not be thrown by the overridden method.

15) 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.

16) How this() is used with constructors?

this() is used to invoke a constructor of the same class

17) How super() used with constructors?

super() is used to invoke a super class constructor

Package and Interface


18) What are packages ? what is use of packages ?

The package statement defines a name space in which classes are stored.If you omit the package, the classes are put into the default package. Signature... package pkg; Use: * It specifies to which package the classes defined in a file belongs to. * Package is both naming and a visibility control mechanism

19) What is difference between importing "java.applet.Applet" and "java.applet.*;" ?

java.applet.Applet" will import only the class Applet from the package java.applet Where as "java.applet.*" will import all the classes from java.applet package.

20) What do you understand by package access specifier?

public: Anything declared as public can be accessed from anywhere private: Anything declared in the private can’t be seen outside of its class. default: It is visible to subclasses as well as to other classes in the same package.

21) What is interface? What is use of interface?

It is similar to class which may contain method’s signature only but not bodies. Methods declared in interface are abstract methods. We can implement many interfaces on a class which support the multiple inheritance.

22) Is it is necessary to implement all methods in an interface?

Yes. All the methods have to be implemented.

23) Which is the default access modifier for an interface method?

public

24) Can we define a variable in an interface ?and what type it should be ?

Yes we can define a variable in an interface. They are implicitly final and static.

25) What is difference between interface and an abstract class?

All the methods declared inside an Interface are abstract. Where as abstract class must have at least one abstract method and others may be concrete or abstract. In Interface we need not use the keyword abstract for the methods.

26) How many concrete classes can you have inside an interface?

None

27) Can you extend an interface?

Yes

28) If you do not implement all the methods of an interface while implementing , what specifier should you use for the class ?

abstract

29) How do you achieve multiple inheritance in Java?

Using interfaces.

30) How to declare an interface example?

access class classname implements interface.

31) Can anonymous classes be implemented an interface?

Yes.

32) Name interfaces without a method?

Serializable, Cloneble & Remote.

33) Is it possible to use few methods of an interface in a class ? If so, how?

Yes. Declare the class as abstract.