Oops Concepts

Object Oriented Programming System.

  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation
These are the oops concepts, Let us discuss in deep.


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.
 
Class:

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

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


 
Polymorphism

       When one task is performed by different ways i.e. known as polymorphism. For example: to convenes the customer differently, to draw something e.g. shape or rectangle etc.


Note: In java, we use method overloading and method overriding to achieve polymorphism.

      Another example can be to speak something e.g. cat speaks mew, dog barks woof etc.
 

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.


 
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.


Example:

class Student1
{
        int id;                    //data member (also instance variable)
        String name;       //data member(also instance variable)
   
public static void main(String args[])
   {
    Student1 s1=new Student1();     //creating an object of Student
    System.out.println(s1.id);
    System.out.println(s1.name);
   }
}

Instance variable:

          The Instance variable created in side the Class but outside of the method. This Instance variable doesn't get any memory in compile time. But while run-time, object(instance) is created get memory that's why this is called as Instance Variable.

Method:

     In Java, Method is an used to expose the behavior of the object. This is similar to the function, but Java it's called method.

new - new is a keyword used to allocate the memory.

Another example:
    class Student2{ 
     int rollno; 
     String name; 
     
     void insertRecord(int r, String n){  //method 
      rollno=r; 
      name=n; 
     } 
     
     void displayInformation(){System.out.println(rollno+" "+name);}//method 
     
     public static void main(String args[]){ 
      Student2 s1=new Student2(); 
      Student2 s2=new Student2(); 
     
      s1.insertRecord(8017,"Sathish"); 
      s2.insertRecord(8034,"Yogesh"); 
     
      s1.displayInformation(); 
      s2.displayInformation(); 
     
     } 
    }
Output:

8027 Sathish
8034 Yogesh
__________________________________________________________________________________
Creating multiple objects by one type only

We can create multiple objects by one type only as we do in case of primitives.

    Rectangle r1=new Rectangle(),r2=new Rectangle();  //creating two objects 
    class Rectangle{ 
     int length; 
     int width; 
     
     void insert(int l,int w){ 
      length=l; 
      width=w; 
     } 
     
     void calculateArea(){System.out.println(length*width);} 
     
     public static void main(String args[]){ 
      Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects 
       
      r1.insert(11,5); 
      r2.insert(3,15); 
     
      r1.calculateArea(); 
      r2.calculateArea(); 
    } 
    } 

Output:
55
45    




No comments:

Post a Comment