/** * Created by PhpStorm. * User: Rahul * Date: 12/10/2015 * Time: 2:43 AM */ OOP's Concept

Object oriented Programing

Core Java

Three pillar of OOP's

Three pillar of OOP's Encapsulation, Inheritance and Polymorphism.


Binding the data and corresponding method into a single module is called Encapsulation
Encapsulation refers to keeping the data and method inside a class such users do not access the data directly but via the public methods. Encapsulation = Datahiding + Abstraction
In OO Design, it is desirable to design classes that are tightly encapsulated, loosely coupled and highly cohesive, so that the classes are easy to maintain and suitable for re-use. we will discuss about the cohesion and coupling later.

Data Hiding

In this sample code, the class Student members variables such as name, age, addr1, etc can not be accessed directly. You need to go through the interfaces functions to access these members.

Example of Data Hiding : with source code

import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;

class Student
      private String name;
      private int age;
      private String addr1;
      private String addr2;
      private String city;
      private String zipcode;

      public Student() { }

      public String GetName() { return name; }
      public String GetAddr1() { return addr1; }
      public String GetAddr2() { return addr2; }
      public String GetCity() { return city; }
      public String GetZipCode() { return zipcode; }
      public int GetAge() { return age; }

      public void SetName(String s) { name = s; }
      public void SetAddr1(String s) { addr1 = s; }
      public void SetAddr2(String s) { addr2 = s; }
      public void SetCity(String s) { city = s; }
      public void SetZipCode(String s) { zipcode = s; }
      public void GetAge(int v) { age = v; }

class DataHiding
      static public void main(String[] args)
            Student student = new Student();

            // student.name = "javaEssential.com"; // Not allowed, compiler error name has private access in Student
            // System.out.print(student.name);

D:\Program Files\Java\jdk1.6.0_23\bin>javac DataHiding.java

D:\Program Files\Java\jdk1.6.0_23\bin>java DataHiding


Hiding internal implementation and just highlight the set of services what we are offering is called abstraction.
Using the abstract keyword we can Declare class and method abstract.
to declare a class abstract the keyword appears in the class declaration somewhere before the class keyword.
An abstract class is something which is incomplete and you can not create instance(object) of abstract class. If you want to use it you need to make it complete or concrete by extending it. A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended.

Frequently changing properties and methods can be grouped to a separate type so that the main type need not under go changes. This adds strength to the OOAD principle -"Code should be open for Extension but closed for Modification".

When do you use abstraction ? ( most important to Understand ) when you know something needs to be there but not sure how exactly it should look like or how it implement.
e.g. when I am creating a class called Vehicle, I know there should be methods like start() and Stop() but don't know start and stop mechanism of every vehicle since they could have different start and stop mechanism e..g some can be started by kick or some can be by pressing buttons .

Example of Abstraction - abstract class and abstract method example

	abstract public class Shape {
	   // Private member variable
	   private String color;

	   // Constructor
	   public Shape (String color) {
	      this.color = color;

	   @Override // why we override toString is explained in Collection topic
	   public String toString() {
	      return "Shape of color=\"" + color + "\"";

	   // All Shape subclasses must implement a method called getArea()
	   abstract public double getArea();

public class TestShape {
   public static void main(String[] args) {
      Shape s1 = new Rectangle("red", 4, 5);
      System.out.println("Area is " + s1.getArea());

      Shape s2 = new Triangle("blue", 4, 5);
      System.out.println("Area is " + s2.getArea());

      // Cannot create instance of an abstract class
      Shape s3 = new Shape("green");   // Compilation Error!!

OOP_SoccerGame.png summary, an abstract class provides a template for further development. The purpose of an abstract class is to provide a common interface (or protocol, or contract, or understanding, or naming convention) to all its subclasses. For example,
in the abstract class Shape, you can define abstract methods such as getArea() and draw().
No implementation is possible because the actual shape is not known.
However, by specifying the signature of the abstract methods, all the subclasses are forced to use these methods' signature. The subclasses could provide the proper implementations.

you can not create instance(object) of abstract class