Object Oriented Programming (OOPs) concept in Java - ayushmathur94/InterviewSpecificPrep Wiki

Original URL: https://github.com/ayushmathur94/InterviewSpecificPrep/wiki/Object-Oriented-Programming-(OOPs)-concept-in-Java

Q1.) What are concepts of Object Oriented Programming (OOPs) ?

1.   Absraction 
2.   Encapsulation
3.   Polymorphism 
4.   Inheritance
5.   Association
6.   Composition
7.   Aggregation

Q2.) What is Abstraction ?

Abstraction is the concept of hiding of internal implementation while showing only basic interface/functionality 
to the user. 
For example, a method that adds two integers. The internal processing of the method is hidden from the outer 
world.  

Ways to Achieve Abstraction :   1.) Using Abstract Class        2.) Using Interface

Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited 
from another class). Abstract class can have abstract as well as non-abstract methods.

Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by 
the subclass (inherited from).

Example of abstraction via abstract class

Another way to achieve abstraction in Java, is with interfaces.

Using interfaces, you can achieve (complete) abstraction. Since all the methods of the interface are 
abstract and the user doesn’t know how a  method is written except the method signature/prototype. 

Q3.) What is an interface in Java ?

> The methods declared in interface are by default abstract (only method signature, no body).  To access the 
interface methods, the interface must be "implemented" (kinda like inherited) by another class with the 
implements keyword (instead of extends). The body of the interface method is provided by the "implement" class.
> The class that implements interface must implement/have all the methods of that interface.

Example of abstraction via Inferface

> The variables declared in an interface are public, static & final by default. 

> Java programming language does not allow you to extend more than one class, However you can implement more
than one interfaces in your class.

Example of Abstraction via More Than One Interfaces

Q4.) What is difference between abstract class and interface ? absVsInt

Q5.) What is Encapsulation ?

> Encapsulation in Java is a process of wrapping data and code (function/method) together under a single unit.
> It’s the mechanism to bind together the data and the function that work on it.
> Encapsulation is the process of hiding Objects’ properties from the outer world and provides methods
  to access them.
> Encapsulation hides the objects’ data, so it’s also called data-hiding.
> Encapsulation is one of the ways to achieve abstraction in Object-oriented programming.

Q6.) What are the ways to achieve encapsulation in JAVA ?

We can achieve encapsulation in Java by declaring all class fields as private and then provide public getter and 
setter methods for them.
 
 public class Data {

 private int id;
 private String value;

 public int getId() {
	return id;
 }

 public void setId(int id) {
	this.id = id;
 }

 public String getValue() {
	return value;
 }

 public void setValue(String value) {
	this.value = value;
 }

   }

Q7.) What are advantages of Encapsulation ?

  > code is loosely coupled  (We can change the class variable  name (not instance variable) without affecting 
   the client programs, by depreciating old methods and providing new getter and setters with new class variable
   names)
  > code is more secure 
  > code reusability 
  > helps provide access control for Object properties

Q8.) What is Inheritance ?

  > Inheritance in java is one of the core concepts of Object Oriented Programming.
  > Its a mechanism by which one class is allowed to inherit all the properties/features and behavior (ie. fields
    and methods) of another class (parent class).
  > Java Inheritance is used when we have is-a relationship between objects.
  > Inheritance in Java is implemented using extends keyword.
  
  Terminologies related to inheritance :
  Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
  Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, 
  or child class). The subclass can add its own fields and methods in addition to superclass fields and methods.
  
   class derived-class extends base-class{
         // methods and fields
    }

 > by using the object of the subclass we can also access the members of a superclass. 

Example of Inheritance in Java

Q9.) What are the types of Inheritance in Java ?

 1.) Single Inheritance  2.) MultiLevel Inheritance  3.) Hierarchical Inheritance  4.) Multiple Inheritance 
 5.) Hybrid Inheritance

 1.) Single Inheritance : 
  In Single Inheritance , subclasses inherit features of (only) one superclass. 

 2.) MultiLevel Inheritance : 
   In MultiLevel Inheritance , a derived class will be inheriting a parent class and as well as the derived class 
   also act as the parent class to other class.

 3.) Hierarchical Inheritance:
   In Hierarchical Inheritance, one parent class will be inherited by many sub classes.
   ClassA will be inherited by ClassB, ClassC and ClassD

 4.) Multiple Inheritance :
  > In multiple Inheritance , one class can have more than one parent class and inherit features from 
   all parent classes.
  > Multiple Inheritance is nothing but one class extending more than one class.
  > Multiple Inheritance is not directly supported by Java. But you can achieve Multiple Inheritance in Java 
   using Interfaces.

 5.) Hybrid Inheritance : 
  > Hybrid Inheritance is the combination of Single and Multiple Inheritance. 
  > ClassA will be acting as Parent Class for ClassB and ClassC , & ClassB and Class C will be acting as Parent 
   for ClassD.
  > Hybrid Inheritance is not directly supported by Java.But you can achieve Hybrid inheritance in Java 
   using Interfaces.

Q.) What is Polymorphism ?

Q.) What are the types of Polymorphism ?

    There are two types of polymorphism in Java: ***compile-time polymorphism*** and ***runtime polymorphism***

    Compile Time Polymorphism: Whenever an object is bound with their functionality at the compile-time, this is known as the compile-time 
    polymorphism. At compile-time, java knows which method to call by checking the method signatures. So this is called compile-time polymorphism 
    or static or early binding. Compile-time polymorphism is achieved through method overloading. Method Overloading says you can have more than 
    one function with the same name in one class having a different prototype. Function overloading is one of the ways to achieve polymorphism but 
    it depends on technology that which type of polymorphism we adopt. In java, we achieve function overloading at compile-Time.

    Run-Time Polymorphism: Whenever an object is bound with the functionality at run time, this is known as runtime polymorphism. The runtime 
    polymorphism can be achieved by method overriding. Java virtual machine determines the proper method to call at the runtime, not at the compile 
    time. It is also called dynamic or late binding. Method overriding says child class has the same method as declared in the parent class. It 
    means if child class provides the specific implementation of the method that has been provided by one of its parent class then it is known as 
    method overriding.

difference bw runtime and compiletime polymorphism

Q.) How Inheritance and polymorphism are connected ?

     Polymorphism occurs when we have many classes that are related to each other by inheritance.
     Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows 
     us to perform a single action in different ways.
     For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, 
     Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.)

Q.) What is Association , Aggregation and Composition ?

Best Described AssociationAggComp