OOPS Concept in Java

OOPS Concept in Java

1. What is OOPS?
A:Object Oriented Programming System is a programming technique to create programs based on the real world objects. The states and behaviors of an object are represented as the member variables and methods. In object oriented programming the programs are organized around objects and data rather than actions and logic.

2. What is the advantage of OOP?

A:The major advantages of OOP are:

Simplicity:Software objects model real world objects, so the complexity is reduced and the program structure is very clear.
Modularity:Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
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.
Extensibility:Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
Maintainability:Objects can be maintained separately, making locating and fixing problems easier.
Reusability:Objects can be reused in different programs.

3. What is the difference between Procedural and OOPS?

A:Procedural language is based on functions but object oriented language is based on real world objects.
Procedural language gives importance on the sequence of function execution but object oriented language gives importance on states and behaviors of the objects.
Procedural language exposes the data to the entire program but object oriented language encapsulates the data.
Procedural language follows top down programming paradigm but object oriented language follows bottom up programming paradigm.
Procedural language is complex in nature so it is difficult to modify, extend and maintain but object oriented language is less complex in nature so it is easier to modify, extend and maintain.
Procedural language provides less scope of code reuse but object oriented language provides more scope of code reuse.

4. Is Java a completely Object Oriented Programming Language? How will you consider Java as an OO programming language?

A:In object oriented programming language the predefined and user defined data types must be an object but Java supports primitive types so Java is not considered as a completely object oriented programming language. Also Java does not support multiple inheritance so it is not a completely object oriented programming language.

5. What are the basic concepts of OOPS?

A:There are four basic concepts of OOPS:

6.What’s a Class?

A:A Class describes all the attributes of object, as well as the methods that implements the behavior of member object. That means this is a template or blue print.
In object-oriented programming , a class is a template definition of the method s and variable s in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables.

7.What’s an Object?

A:Object is an instance of a class, it contains real values instead of variables.

Person employee=new Person();

8.What’s the relation between Classes and Objects?

A:Class is a defenition , where as object is an instance of a class created.Class is a blue print while Objects are actual objects existing in the real world.
9.What is Abstraction?

A:Abstraction is separating the functions and properties that logically can be separated to a separate entity.
Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object.

10. What is Encapsulation?

A:Encapsulation is an OOPS concept to create and define the permissions and restrictions of an object and its member variables and methods. A very simple example to explain the concept is to make the member variables of a class private and providing public getter and setter methods. Java provides four types of access level modifiers: public, protected, no modifier and private.

11. How to make an object completely encapsulated in Java?

To make an object completely encapsulated in Java we need to declare all the member variables as private and provide public getter and setter methods for them.

12. What is the difference between Abstraction and Encapsulation?

 A:“Program to interfaces, not implementations” is the OO principle for Abstraction and “Encapsulate what varies” is the OO principle for Encapsulation.
    Abstraction provides a general structure of a class and leaves the details for the implementers. Encapsulation is to create and define the permissions and restrictions of an object and it member variables and methods.
    Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using four types of access level modifiers: public, protected, no modifier and private.

13. What is Polymorphism?

A:Polymorphism is the occurrence of something in various forms. Java supports various forms of polymorphism like polymorphic reference variables, polymorphic method, polymorphic return types and polymorphic argument types.

Polymorphic reference variables can hold any of its subtype class reference.
class A extends C{}
class B extends C{}
C cType = new A();
cType = new B();

Polymorphic method can be overloading or overriding.

Polymorphic return types and polymorphic argument types:

public class Poly {
    public static void main(String[] args) {
        setNumber(new Integer(1));

    public static Number getNumber() {
        Integer i = new Integer(1);
        return i;

    public static void setNumber(Number n) {

14.What is Inheritance ?

A:In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects.
A class can inherit attributes and behavior from pre-existing class called base classes, superclasses, or parent classes. The resulting class is known as derived classes, subclasses, or child classe

15. What is multiple inheritance?

A:A child class inheriting states and behaviors from multiple parent classes is known as multiple inheritance.

16.What is Overloading ?

A:Overloading is one type of polymorphism. Overloading allows an object to have different meanings depending on its context. The term is used most often in reference to operators that can behave differently depending on the data type, or class, of the operands.

17.How do you implement inheritance in Java?

A:In Java, Inheritance is Implemented by "EXTEND" keyword.

public class employee extends person {


18.How can we implement polymorphism in Java?

A:Java has excellent support of polymorphism in terms of
a) Method Polimorphism through overloading and overriding.
b) Object polymorphism through inheritance / Interface

19.Define overriding in Java?

A:when you extend a class and write a method in the derived class which is exactly similar to the one present in the base class, it is termed as overriding.

public class Vehicle {
    public String getEngineType(){
        //some code here
public class Vehicle {
public String getEngineType(){
        //some new code here

As you can see, in the class Vehicle , we have overridden the method present in the BaseClass (Vehicle ) with a completely new piece of code.

20.Define overloading in Java?

A:when you have more than one method with the same name but different arguments, the methods are said to be overloaded.

Public void saveUserInfo(int userId){

public void saveUserInfo(String userName,String Location){


21.Differenciate method overriding and overloading in Java ?

A:Method overloading deals with the notion of having two or more methods(functions) in the same class with the same name but different arguments.

While Method overriding means having two methods with the same arguments, but different implementation. One of them would exist in the Parent class (Base Class) while another will be in the derived class(Child Class).@Override annotation is required for this.

22.What’s an interface ?

A: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);

23.How can we implement an interface in java ?

A: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

24.What is an Abstract class ?

A: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 {


25.What are Abstract methods ?

A:An abstract class can have abstract methods. You declare a method abstract by adding the abstract keyword in front of the method declaration.
Adbtract 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();

26.What’s the difference between 'Abstract' classes and 'Interfaces' ?

A:1) Absract Class can only be inherited, while interface cannot be. Interface has to be implemented.
2) Abstract class can have implementation ,where as interface cannot implement methods.

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

A: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 declaraton
    public static int userCount; // Non Static variable declaration

28.What are inner classes ?

A: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.

29.Why we are using inner classes ?

A: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.

30.What are packages ?

A: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 {


31.What is a constructor in class ?

A: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(int count){


Post a Comment