Monday, December 4, 2017

Core Java OOPS

Object-Oriented Programming 
          Object Oriented Programming ( OOP ) is at the Core of Java. Whenever you write any simple or complex program in Java. It always follows OOP principle indirectly.


Four OOP Principles

All object-oriented programming languages provide mechanisms that help you implement the object-oriented model. They are Abstraction, encapsulation, inheritance, and polymorphism


A)   Abstraction : 
An essential element in OOP is Abstraction. Humans manage complexity through Abstraction. Data Abstraction is the property by virtue of which only the essential details are displayed to the user. It can achieve by classes and interfaces. 
Suppose Person want to purchase a new mobile phone from a particular shopping mall. He will drive to the shopping mall in his car.  While driving he doesn't want to concentrate on which car part is helping to move car ahead and how the petrol gets converted into piston energy instead he just needs to think about which mobile he want to purchase. From the outside, a car is a single object. Once you inside the car you can see the car consist of several ecosystems. Steering, brake, engine, accelerator, CD player
A powerful way to manage abstraction is through the use of hierarchical classifications.
This allows you to layer the semantics of complex systems, breaking them into more manageable pieces.

Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user
In the class, we can 2 things define abstract class/method and interfaces. So whoever wants to implement them, can be achieved by doing their own implementation way. 

B)   Encapsulation
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. It keeps both safe from outside interference and misuse. One way to think about 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.

In Java, the basis of encapsulation is the class. A class defines the structure and behavior (data and code) that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class,

 It is also known as data hiding.
To achieve encapsulation in Java −
·        Declare the variables of a class as private.
·        Provide public setter and getter methods to modify and view the variables values.

Image result for abstraction vs encapsulation

Wrapping Data into single Unit. Encapsulation simply means binding object state (fields) and behavior (methods) together. If you are creating class, you are doing encapsulation.

Inheritance
Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification.




1.   Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.
2.   Inheritance allows us to reuse of code, it improves reusability in your java application.
3.   The parent class is called the base class or super class. The child class that extends the base class is called the derived class or sub class or child class.
extends is the keyword used to inherit the properties of a class.

The super keyword is similar to this keyword.


IS-A Relationship
IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
public class Car {
}

public class Suzuki extends Car  {
}

public class Tata extends  Car {
}

public class Nexa  extends Suzuki {
}
Now, based on the above example, in Object-Oriented terms, the following are true −
  • Car is the superclass of Suzuki class.
  • Car is the superclass of Tata class.
  • Suzuki and Nexa are subclasses of Car class.
  • Types of Inheritance:
    Single Inheritance: refers to a child and parent class relationship where a class extends the another class.
  • Multilevel inheritance: refers to a child and parent class relationship where a class extends the child class. For example class A extends class B and class B extends class C.
  • Hierarchical inheritance: refers to a child and parent class relationship where more than one classes extends the same class. For example, class B extends class A and class C extends class A.
  • Multiple Inheritance: refers to the concept of one class extending more than one classes, which means a child class has two parent classes. Java doesn’t support multiple inheritance, read more about it here.
Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. 



Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature
More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” This means that it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is the compiler’s job to select the specific action (that is, method) as it applies to each situation. You, the programmer, do not need to make this selection manually. You need only remember and utilize the general interface


Types of Polymorphism
1) Static Polymorphism
2) Dynamic Polymorphism

Static Polymorphism:

Polymorphism that is resolved during compiler time is known as static polymorphism. Method overloading can be considered as static polymorphism example.
Method Overloading: This allows us to have more than one methods with same name in a class that differs in signature.

Dynamic Polymorphism

It is also known as Dynamic Method Dispatch. Dynamic polymorphism is a process in which a call to an overridden method is resolved at runtime rather, thats why it is called runtime polymorphism.

IS-A & HAS-A Relationships

A Car IS-A Vehicle and HAS-A License then the code would look like this:
public class Vehicle{ }
public class Car extends Vehicle{
   private License myCarLicense;
}

Abstract Class and methods in OOPs Concepts

Abstract method:
1) A method that is declared but not defined. Only method signature no body.
2) Declared using the abstract keyword
3) Example :
abstract public void playInstrument();
5) Used to put some kind of compulsion on the class who inherits the class has abstract methods. The class that inherits must provide the implementation of all the abstract methods of parent class else declare the subclass as abstract.
6) These cannot be abstract
·         Constructors
·         Static methods
·         Private methods
·         Methods that are declared “final”

Interfaces in Java

An interface is a blueprint of a class, which can be declared by using interface keyword. Interfaces can contain only constants and abstract methods (methods with only signatures no body).Like abstract classes, Interfaces cannot be instantiated, they can only be implemented by classes or extended by other interfaces. Interface is a common way to achieve full abstraction in Java.
1.    Java does not support Multiple Inheritance, however a class can implement more than one interfaces
2.    Interface is similar to an abstract class but it contains only abstract methods.
3.    Interfaces are created by using interface keyword instead of the keyword class
4.    We use implements keyword while implementing an interface(similar to extending a class with extends keyword)


Generalization and Specialization:
In order to implement the concept of inheritance in an OOPs, one has to first identify the similarities among different classes so as to come up with the base class.
This process of identifying the similarities among different classes is called Generalization. Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes or methods.
In contrast to generalization, specialization means creating new subclasses from an existing class. If it turns out that certain attributes or methods only apply to some of the objects of the class, a subclass can be created.