Advantage of OOP in java

In this page, we will learn about basics of OOPs. Object Oriented Programming is a paradigm that provides many concepts such as inheritance, data binding, polymorphism etc. Simula is considered as the first object-oriented programming language. The programming paradigm where everything is represented as an object, is known as truly object-oriented programming language. Smalltalk is considered as the first truly object-oriented programming language.

★ Object

★ Class

★ Inheritance

★ Polymorphism

★ Abstraction

★ Encapsulation



★ Object :

Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical.

A. Using new keyword
This is the most common way to create an object in java. Almost 99% of objects are created in this way.
  MyObject object = new MyObject();

B. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
  MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

C. Using clone()
The clone() can be used to create a copy of an existing object.
  MyObject anotherObject = new MyObject();
  MyObject object = (MyObject) anotherObject.clone();

D. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
  ObjectInputStream inStream = new ObjectInputStream(anInputStream );
  MyObject object = (MyObject) inStream.readObject();


★ Class :

Collection of objects is called class. It is a logical entity.

  public class Puppy{
     int puppyAge;
     public Puppy(String name){
     // This constructor has one parameter, name.
     System.out.println("Name chosen is :" + name );
  }
     public void setAge( int age ){
     puppyAge = age;
     }
     public int getAge( ){
     System.out.println("Puppy's age is :" + puppyAge );
     return puppyAge;
     }
     public static void main(String []args){
     /* Object creation */
     Puppy myPuppy = new Puppy( "tommy" );
     /* Call class method to set puppy's age */
     myPuppy.setAge( 2 );
     /* Call another class method to get puppy's age */
     myPuppy.getAge( );
     /* You can access instance variable as follows as well */
     System.out.println("Variable Value :" + myPuppy.puppyAge );
     }
  }

  Output:-
  Name chosen is :tommy
  Puppy's age is :2
  Variable Value :2


★ Inheritance :

When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.

  public class A {
     public A() {
     System.out.println("Hi!");
     }
  }
  public class B extends A {
     public B() {
     System.out.println("Bye!");
     }
     public static void main(String[] args) {
     B b = new B();
     }
  }

  Output:-
  Hi!
  Bye!


★ Polymorphism :

When one task is performed by different ways i.e. known as polymorphism. For example: to convense the customer differently, to draw something e.g. shape or rectangle etc.In java, we use method overloading and method overriding to achieve polymorphism.

  class Person
  {
     void walk()
     {
     System.out.println("Can Run....");
     }
  }
  class Employee extends Person
  {
     void walk()
     {
     System.out.println("Running Fast...");
     }
     public static void main(String arg[])
     {
     Person p=new Employee(); //upcasting
     p.walk();
     }
  }

  Output:-
  Running fast...


★ Abstraction :

Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing.In java, we use abstract class and interface to achieve abstraction.Another example can be to speak something e.g. cat speaks meaw, dog barks woof etc.


  public class Main{
     public static void main(String args[]){
     TwoWheeler test = new Honda();
     test.run();
     }
  }
  abstract class TwoWheeler {
     public abstract void run();
  }
  class Honda extends TwoWheeler{
     public void run(){
     System.out.println("Running..");
     }
  }


★ Encapsulation :

Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.


  class Employee{
     private int employeeId;
     private String employeeName;
     private String designation;
     public int getEmployeeId() {
       return employeeId;
     }
     public void setEmployeeId(int employeeId) {
       this.employeeId = employeeId;
     }
     public String getEmployeeName() {
       return employeeName;
     }
     public void setEmployeeName(String employeeName) {
       this.employeeName = employeeName;
     }
     public String getDesignation() {
       return designation;
     }
     public void setDesignation(String designation) {
       this.designation = designation;
     }
  }