Java Interview Questions - OOP

Here is the Second set of Java Interview Questions which focuses on the Object Oriented Programming principles. Please comment if you want to add any other question to this list.

1. What is OOP?
OOP is the common abbreviation for Object-Oriented Programming. Object oriented programming organizes a program around its data, i. e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.
There are four main principals of oops which are called Abstraction, Polymorphism, Inheritance and Encapsulation.
2. What is the difference between procedural and object-oriented programs?
In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code.
In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code.
3. What is Abstraction?
Abstraction is the process of generalization by reducing the information content of an entity, typically to retain only information which is relevant for a particular purpose. For example, an Employee has various attributes like name, skin color, age, hair color, etc. But we consider only those attributes of employees which are required and relevant to program a system.
In general, abstraction is the process of focusing on important thing without including background details in program.
4. Explain the Encapsulation principle.
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
5. Explain the Inheritance principle.
Inheritance is the process by which one object acquires the properties of another object. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses.
6. Explain the Polymorphism principle.
The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".
From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:
  • Method overloading
  • Method overriding through inheritance
  • Method overriding through the Java interface
7. What is the advantage of OOP?
The Major advantages of OOP are:
  1. Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear.
  2. Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system.
  3. Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
  4. Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
  5. Maintainability: objects can be maintained separately, making locating and fixing problems easier.
  6. Re-usability: objects can be reused in different programs.
8. What is the Difference between Class and Object?
A Class is actually a blueprint or a template to create an Object. Whereas an Object is a an actual instance of a Class. For example Employee is a class, while John is a real employee which is an Object of Employee Class.
9. What is method overloading and method overriding?
Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading.
Method overriding : When a method in a class having the same method name with same arguments is said to be method overriding.
10. What is difference between overloading and overriding?
  • In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.
  • Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
  • In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
  • Overloading must have different method signatures whereas overriding must have same signature.
11. What is message passing in object oriented programming?
Message passing is a method by which an object sends data to another object or requests other object to invoke method. This is also known as interfacing. It acts like a messenger from one object to other object to convey specific instructions.
12. What are data members and member functions?
Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost and uses functions to operate on these attributes.
The attributes are sometimes called as data members because they hold information. The functions that operate on these data are called as methods or member functions.
Eg: int a,b; // a,b are data members
Void getdata ( ) ; // member function
13. Write the process of programming in an object-oriented language?
  1. Create classes that define objects and their behavior.
  2. Creating objects from class definition.
  3. Establishing communication among objects.
14. What is an Abstract class?
Abstract class defines an abstract concept which can not be instantiated and comparing o interface it can have some implementation while interfaces can not. Below are some points for abstract class:-
We can not create object of abstract class it can only be inherited in a below class.
Normally abstract classes have base implementation and then child classes derive from the abstract class to make the class concrete.
15. What are Abstract methods?
Abstract class can contain abstract methods. Abstract methods do not have implementation. Abstract methods should be implemented in the subclasses which inherit them. So if an abstract class has an abstract method class inheriting the abstract class should implement the method or else java compiler will through an error. In this way, an abstract class can define a complete programming interface thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface. Abstract methods are defined using "abstract" keyword. Below is a sample code snippet.
abstract class MyGraphics {
   abstract void draw();
}
Any class inheriting from 'MyGraphics' class should implement the 'draw' method or else the java compiler will throw an error. So if we do not implement a abstract method the program will not compile.
16. What is the difference between Abstract classes and Interfaces?
Difference between Abstract class and Interface is as follows:-
  • Abstract class can only be inherited while interfaces can not be it has to be implemented.
  • Interface cannot implement any methods, whereas an abstract class can have implementation.
  • Class can implement many interfaces but can have only one super class.
  • Interface is not part of the class hierarchy while Abstract class comes in through inheritance.
  • Unrelated classes can implement the same interface.
17. What is difference between Static and Non-Static fields of a class?
Non-Static values are also called as instance variables. Each object of the class has its own copy of Non-Static instance variables. So when a new object is created of the same class it will have completely its own copy of instance variables.
Static values have only one copy of instance variables and will be shared among all the objects of the class.
18. What are inner classes and what is the practical implementation of inner classes?
Inner classes are nested inside other class. They have access to outer class fields and methods even if the fields of outer class are defined as private.
Normally inner classes are used for data structures or some kind of helper classes.
19. What is a constructor in class?
Constructor has the same name as the class in which it resides and looks from syntax point of view it looks similar to a method. Constructor is automatically called immediately after the object is created, before the new operator completes. Constructors have no return type, not even void. This is because the implicit return type of a class' constructor is the class type itself. It is the constructor's job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.
20. Can constructors be parameterized?
Yes we can have parameterized constructor which can also be termed as constructor overloading. Below is a code snippet which shows two constructors for MathsPI class one default (parameter-less) and other with parameter.
class MathsPI {
   double PI;
   // This is the constructor for the maths constant class.
   MathsPI() {
      PI = 3.14;
   }
   MathsPI(int pi) {
      PI = pi;
   }
}
21. What are access modifiers?
Access modifiers decide whether a method or a data variable can be accessed by another method in another class or subclass.
four types of access modifiers:
  • Public - Can be accessed by any other class anywhere.
  • Protected - Can be accessed by classes inside the package or by subclasses ( that means classes who inherit from this class).
  • Private - Can be accessed only within the class. Even methods in subclasses in the same package do not have access.
  • Default - (Its private access by default) accessible to classes in the same package but not by classes in other packages, even if these are subclasses.
22. What is runtime polymorphism or dynamic method dispatch?
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
23. What is Dynamic Binding?
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
24. How do you prevent a method from being overridden?
To prevent a specific method from being overridden in a subclass, use the final modifier on the method declaration, which means "this is the final implementation of this method", the end of its inheritance hierarchy.
25. Do interfaces have member variables?
Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
 

Copyright © 2012 | ScriptSplash | Powered by Blogger Templates