OOP  Concept


OOP is a programming style which concept use to map the real world scenario by using some primary concepts such as abstraction, encapsulation, inheritance, and polymorphism. Most popular programming language like Java , c++ , c# , Ruby etc. Follow an object oriented programming paradigm.

Abstraction

When create a class which is declared as an abstract, cannot create an object of an abstract  class. 
To use an abstract class, you have to inherit it from another class where you have to provide implementation for the abstract methods there itself , else it will become an abstract class.

Let's look at the syntax of an abstract class:


Abstract class Mobile {   // abstract class mobile
Abstract void run();      // abstract method

Encapsulation

It uses for data hiding and data encapsulation is a mechanism of bundling the data. Declare the variables of a class as a private. When provides the public getter and setter methods to modify and view the variables values.

Let's look at the syntax of an Encapsulation class:

public class EncapTest {
   private String name;
   private String idNum;
   private int age;
 
   public int getAge() {
      return age;
   }
 
   public String getName() {
      return name;
   }
 
   public String getIdNum() {
      return idNum;
   }
 
   public void setAge( int newAge) {
      age = newAge;
   }
 
   public void setName(String newName) {
      name = newName;
   }
 
   public void setIdNum( String newId) {
      idNum = newId;
   }
}

Inheritance

Inheritance is one such concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes.In Java , there are two classes:
  1. Parent class (Super or Base class).
  2. Child class (Subclass or Derived class).
Let's look at the syntax of an Encapsulation class:

Class A
{
---
}
Class B extends A {
---
}

Polymorphism


Polymorphism is the ability of an object to take on many forms. 
Java allows to implements polymorphism in tow ways.
  1. Method Overriding.
  2. Method Overloading.

Method Overriding.

It is Dynamic and run  time poymorphism. Method Overriding means that  one of the method in supper class that redifined in the  sub-class. In this case method signature will not change. Only the implementation part will change.

Let's look an Example:


class Shape{  
void draw()
{
System.out.println("drawing...");}  
}  
class Rectangle extends Shape{  
void draw()
{
System.out.println("drawing rectangle...");} 
}  
class Circle extends Shape{  
void draw()
{
System.out.println("drawing circle...");}  
}  

Method Overloading.

It is Static and Compile time polymorphism.  Method Overloading is in same class , where more  than  one method can have same name with different signatures.

Method signature can be changed using

            1. Number of  parameters.
                         Sum(int , int)
                         Sum(int , int , int )

           2.Datatype of  parameters.
                          Sum(int , int)
                         Sum(int , float )

           3.Sequence  of  parameters.
                          Sum(float , int)
                         Sum(int , float )

Comments

Popular posts from this blog

Using OAuth 2.0 Authorization framework