OOPS - sivakrsna/Java-Interview GitHub Wiki

Object Oriented Programming

  • It is programming paradigm based on the concept of object, which may contains 'data' in the form of fields, often known as Attributes and Code, in the form of procedures, known as methods.
  • It is programming that uses classes and objects to create models based on the real world entities.
  • By oops, objects procedures(functions) can access and modify the data fields of the objects with which they are associated
  • It organizes a program around its data(i.e objects) and a set of well-defined interfaces to that data.
  • Objects are designed to interact with other objects
  • Oops can be characterized as "data controlling access to the code"

Programming Paradigms

  • All computer programs contains of two elements
  1. Code
  2. Data
  • A program can be conceptually organized around its code(what is happening) or around its data(who is being affected)

Programming Paradigms Models

  1. Process Oriented Model
    • What is happening
    • Code acting on data
    • Eg. C
  2. Object Oriented Model
    • Who is being affected
    • Data controlling access to code
    • Eg. Java

OOPS Concepts

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritence

Class

  • A class is a template for an object.
  • It is blueprint or prototype from which objects are created.
  • It is a logical component but the physical entity
  • It defines a new data type, once defined, this new type can be used to create objects of that type.

Object

  • An Object is an instance of Class
  • It is an entity that has state and behavior.
  • It is real world and run time entity.
  • Object has two characteristics
    1. State - Object stores its state in fields(variables)
    2. Behavior - Exposes its behavior through methods(functions)

Abstraction

  • It is a concept of hiding the internal details and describing things in simple terms, to show functionality to user.

  • We use abstract classes and interfaces to achieve abstraction For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior. 0 to 100% abstraction - abstract classes 100% abstraction - interfaces

  • Abstraction focus on what the object does instead of how it does

  • Shows only relevant data and hide unnecessary details of an object from the user

Abstraction is details hiding(implementation hiding) while Encapsulation is data hiding(Information hiding)

Encapsulation

  • Encapsulation in java is a process of wrapping code and data together into a single unit.
  • Encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper.
  • Access to the code and data inside the wrapper is tightly controlled through a well-defined interface.

Advantages of Encapsulation:

Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is storing values in the variables. He only knows that we are passing the values to a setter method and variables are getting initialized with that value.

Increased Flexibility: We can make the variables of the class as read-only or write-only depending on our requirement. If we wish to make the variables as read-only then we have to omit the setter methods like setName(), setAge() etc. from the above program or if we wish to make the variables as write-only then we have to omit the get methods like getName(), getAge() etc. from the above program

Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.

Testing code is easy: Encapsulated code is easy to test for unit testing.

Polymorphism

  • Polymorphism in java is a concept by which we can perform a single action by different ways.
  • It is feature that allows us to perform a single action in different ways.
  • Polymorphism means more than one form, same object performing different operations according to the requirement.
  • Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. Example

There are two types of polymorphism in java:

  1. Static binding/Compile-Time binding/Early binding/Method overloading.(in same class)
  2. Dynamic binding/Run-Time binding/Late binding/Method overriding.(in different classes)

Static binding

Static binding is a binding which happens during compilation. It is also called early binding because binding happens before a program actually runs.

Method overloading means having two or more methods in the same class with the same name but different arguments.

Dynamic binding

Dynamic binding is a binding which happens during run time. It is also called late binding because binding happens when program actually is running.

Method overriding means having two methods with the same arguments, but different implementations.

Method Overriding in Java is the best example for Runtime Polymorphism. In this type of Polymorphism the Parent class reference can hold object of Parent class or any sub class(Child class) of Parent. This technique is called as Dynamic Method Dispatch

//Assigning Parent class Object to Parent class reference
Parent p = new Parent();
//Assigning Child class Object to Parent class reference
Parent p = new Child();

Dynamic Method Dispatch is a technique in which the overridden method to call is resolved at the run-time rather than at compile time.

In Java to achieve polymorphism a super class reference variable can hold the sub class object.

Inheritence

Inheritance is the process by which one object acquires the properties of another object.

Types of Inheritance in Java

Below are the different types of inheritance which is supported by Java.

Single Inheritance : In single inheritance, subclasses inherit the features of one superclass. In image below, the class A serves as a base class for the derived class B.

Multilevel Inheritance : In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.

Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one sub class.In below image, the class A serves as a base class for the derived class B,C and D.

Multiple Inheritance (Through Interfaces) : In Multiple inheritance ,one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. In image below, Class C is derived from interface A and

Hybrid Inheritance(Through Interfaces) : It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Important facts about inheritance in Java

Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object class.

Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java.

Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods(like getters and setters) for accessing its private fields, these can also be used by the subclass.