Sunday, October 29, 2017

OOPs Concept (Object Oriented Programming) in Java with Examples

OOPs stands for 'object-oriented programming' languages. Java is very popular in the software industry because of the oops concept as well. OOPs concept provides very important concept which is essential in designing and development of java type application.

The main concept provided by oops are -
  • Inheritance
  • polymorphism
  • Encapsulation
  • Abstraction
These concepts are also considered to be the main pillar of the Java programming language because in java each and everything is printed as Object.

  • An object is nothing but an identity which is having state and behavior.
  • State represent characteristics and behavior represent functionality.These are the property of an object.
                                 
                                          

  Example:

        public class Demo

         {

            // State --> Data member --> Variable

                 int house = 2;
                 double cash = 50;
                 int car = 2;

           // Behaviour --> Member function --> Method

            void property () 
                {

                 System.out.println("House" + house);
                 System.out.println("cash" + cash + "lac");
                 System.out.println("Car" + car);


                 }

         }


     
   1. Inheritance : It is one of the oops principle. In this one class acquire the property of another class with the help of a keyword called extend it, is known as inheritance.

  •  A class that is used as the basis of inheritance is called as a superclass or base class.
  • A class that inherit the property from super class is called child class or subclass.
  •  Inheritance implements the IS-A relationship.
    
 Advantages of Inheritance:
  1. It is use for code re-usability.
  2. Reduce the size of source code and increase the code readability.  
  3. Achieve method overriding.
Types: In java there are four types of inheritance.
  1. Single level inheritance
  2. Multiple level inheritance
  3. Multiple inheritance
  4. Hierarchical inheritance

       public class Demo

         {

            // State --> Data member --> Variable

                 int house = 2;
                 double cash = 50;
                 int car = 2;

           // Behaviour --> Member function --> Method

            void property () 
                {

                 System.out.println("House" + house);
                 System.out.println("cash" + cash + "lac");
                 System.out.println("Car" + car);


                 }

         }

    class JrDemo extends Demo

         {


         }

     public class TestFamily
        {
          public static void main (String[] aa ){

           System.out.println("Demo property are:");

           Demo d = new Demo();
           d.property();
           System.out.println("------------------------");
            System.out.println("JrDemo property are:");

           JrDemo  jr = new jrDemo();
           jr.property();


}

        }


Note: Java doesn't support multiple inheritance because one subclass can't acquire property of two super class. If we perform this operation we will get diamond ambiguity.


 



    Diamond ambiguity problem: 

  • This is a problem faced by the compiler during multiple inheritance operation.
  • If class Demo 1 and Demo 2 contains a method that are same name and class Demo 3 try to use that method then compiler will confused from which class that method will call.


  Polymorphism: It is one of the oops principle.
  • One object showing different behaviour at different stages of its life cycle, It is considered as polymorphism.
  • Polymorphism is a latin word where poly stands for many and morphism  stands for forms.
  • In Java polymorphism is classified into two types-              
                  Compile time polymorphism (Static)
                  Runtime polymorphism (Dynamic)

Compile time polymorphism : This polymorphism can be achieve using method overloading.
 When method declaration is gain to get binded to its definition during compilation is called compile time polymorphism.

       
 In below code we have three definition of the same method test() with different parameters would be determined by parameter list at compile time. 


                             class Demo1{

                                      void test(){

                                                     System.out .println("zero argument method");

                                                       }
                                      void test(int a){

                                                     System.out .println("int argument method");
                                                   }

                                     void test(string a){

                                                      System.out .println("string argument method");

                                                  }

                                           }

                                     class TestDemo1{
                         
                                             public static void main(String[] args){

                                                          Demo1 d1 = new Demo1();
                                                          d1.test();
                                                          d1.test(5);
                                                          d1.test("Hello World");
                                                   }

                                        }



Runtime polymorphism: This polymorphism can be achieve using method overriding. When method is get binded to its definition during run time by JVM, It is known as runtime polymorphism. As the binding takes place during execution time, it is also known as late binding.

Example: 

           class Demo1{

                        void wish(){

                                       System.out.println("Good Morning");
                                          }

                           }

            class Demo2 extends Demo1{

                                   void wish(){

                                     System.out.println("Good Evening");

                                           }

                  public static void main(String args[]){

                                Demo1 d1 = new Demo1();
                                d1.wish(); //Call Demo1 method

                                Demo1 d2=new Demo2(); // line 2
                                d2.wish(); // Call Demo2 method // line 3

                                        }
                           }


 Note: When a overridden method call through reference variable of parent class then type of object determine which method would be call. In line 2 we have created reference variable of parent class and in line 3 using this we are calling method. Here object belongs to the child class so child class's method will be called.



Encapsulation: It is one of the oops principle.

  • Protecting the data member of a class with the help of a access specifier called 'private', It is known as Encapsulation.
  • Encapsulation is the concept which used to deal with data member of a class.
  • By declaring data member of a class private, the scope of the member gets bonded within  the class body.
  • The visibility scope is restricted scope is restricted outside the class body. 


                      class Demo {

                                private String username = "John";
                                private  String passowrd = "john123";
                            
                                  }


                      The above class considered to be as encapsulation class, where encapsulated data members are present.

  • The encapsulated data member can not be access from another class direct but it can be access indirectly. Java provides a readymade class 'Java bean class' used to access and manipulate private member of a class i.e. encapsulated member of a class.
  • Encapsulation is also known as “Data hiding”.

public  class student {
     private  String sName;
     private int sAge;
     private String sIdnum;
 
public int getAge{
 
          return sAge;  
}
public String getName(){
          return sName;
            }
public int getIdnum{
 
          return sIdnum;  
}
  
public void setAge (int newAge){
 
         sAge = newAge;
            }
public void setName(String newName){
 
         sName = newName; 
 
            }
public void setId(String newId){
 
         sIdnum = newId;
 
           }

      }
public class EncapStudent{
 
      public static void main(String args[]){
  
  Student s = new Student();
  s.setAge(24);
  s.setName("John");
  s.setId("8648john");
  
System.out.println("Student name is:" + s.getName);
System.out.println("Student age is:" + s.getAge{);
System.out.println("Student id is:" + s.getIdnum);
  
  }
    
}

Note: In the above program variables are declared as private. Setter methods like setAge, setName, setId are used to set values for the variables, getter methods like getAge,getIdnum,getName are used to access these variables.

Abstraction: It is one of the oops principles.
  • "Hiding the implementation code of an application and providing an interface to the end user that only functionality uses available for the user. It is known as abstraction."
  • In the development operation perspective, it is always recommended to hide the implementation code from the end user or customer.
  • An application should be design as such a method, a customer should not understand the internal operations. This type of application can be designed with the help of the abstraction principle.
  • Abstraction in Java can be achieved by abstract class and Interface.


                                                                                                         
TestNG-Annotations






         
     

No comments:

Post a Comment