Tuesday, 9 February 2016

OOP’s Concepts in JAVA

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:

v  Object
v  Class
v  Inheritance
v  Polymorphism
v  Abstraction
v  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.
Class:
Collection of objects is called class. It is a logical entity.
Example:
public class ClassAndObjectDemo {

      public static void main(String[] args) {
            // Creation of an Object. we can creates n number of Objects for a single class
            Sample s = new Sample();
      }
}
class Sample{// class
      int i;
      float j;
      Sample(){
            System.out.println("Sample Class Constructor");
      }
      void m1(){
            System.out.println("sample class m1");

      }
}

Important Point:
1.  For creating an object we are using a 'new' operator
        2. Each object will have all the features of a class

Abstraction:

If we want to hide the implementation to the outside world we are using abstraction. With this technique we are providing security to our implementation.
In JAVA we are achieving this abstraction in two ways
1.       By creating abstract class
2.       By creating Interface

Abstract class:
    An abstract class is a class which will have abstract methods and concrete methods.
            Abstract method: This is a method without implementation
            Concrete method: This is a method which will have implementation
    Ex: Abstract class and its allowed elements in an abstract class

public abstract class AbstractClassDemo {
      // we can create instance variables
      int i;
      int j;
      // we can create constants
      final int k=10;
      // we can create a constructor for abstract class
      public AbstractClassDemo() {
            System.out.println("AbstractClassDemo Constructor");
      }
      // abstract method
      abstract void m1();
      //concrete method
      private void m2(){
           
      }
      // we can write a main method
      public static void main(String[] a){
            System.out.println("Raju");
      }
Important Points:
1.       We are not allowed to create an object for abstract class but we can creates reference to abstract class
   Ex: AbstractClassDemo aCD = new AbstractClassDemo(); // Wrong        
 AbstractClassDemo aCD; // it allowed. Here we are just creating reference
2.       We can’t create object for the abstract class.
3.       We can create reference variable for abstract class.
4.       If a class contains abstract method then we should declare that class as an abstract class.
5.       We can extend abstract class to another class.
6.       If extended class also having abstract class then that class also should declared as abstract class.
7.       Abstract class may contain zero no.of abstract methods. This is for avoiding object creation.

Interfaces:
   An interface will have only abstract methods. So we can call this interface is a fully abstract class
Ex: Interface and allowed elements
public interface InterfaceDemo {
      //we can declare constants
      // if we are not declare "public final static" for constants then compiler by default includes
      public final static int CONSTANT_ONE = 1;
      // if we are not declare "public abstract" for methods then compiler by default includes
      public abstract void methodOne();
      public abstract int methodTwo();
}
Important Points:
1.       We are not allowed to create an object for abstract class but we can creates reference to abstract class
   Ex: InterfaceDemo iD = new InterfaceDemo ();
         
 InterfaceDemo iD;it allowed. Here we are just creating reference

2.       We never highlight our implementation to the outside world so we can achieve security for our implementation.
3.       Interface contains 100% abstract methods.
4.       We can declare constants.
5.       By default these constants ‘public static final’.
6.       By default methods have ‘public abstract’

Inheritance:

   If we want to acquire all the features from other component into our component we will go for inheritance. In JAVA we can achieve this inheritance in two ways
1.       By extending a class : For this we are using “extends” keyword
Ex:
public class ExtendsDemo extends Sample{
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            Sample s = new Sample();
            s.method1();
      }
}
class Sample{
      int method1(){
            System.out.println("I am Sample method1");
            return 'a';
      }
}

Important Points:
1.       We can extend only one class to another class.
     Ex:
            class ExtendsDemo extends Sample
      class ExtendsDemo extends Sample,Sample1
               class ExtendsDemo extends Sample extends Sample1

2.       We can’t achieve multiple inheritance in JAVA using classes because of ambiguity problem

2.    By implementing an interface : 
For this we are using “implements” keyword 
 Ex:

public interface InterfaceDemo {
      //we can declare constants
      // if we are not declare "public final static" for constants then compiler by default includes
      public final static int CONSTANT_ONE = 1;
      // if we are not declare "public abstract" for methods then compiler by default includes
      public abstract void methodOne();
      public abstract void methodTwo();
}
class ImplementsDemo implements InterfaceDemo{
      public void methodOne(){
            System.out.println("methodOne Implementation");
      }
      // if we are not interested to provide implementation then we should provide an empty implementation at least
      public void methodTwo(){

      }
}

Important Points:
1.       We can achieve multiple inheritance by using interfaces
Ex:
class ImplementsDemo implements InterfaceDemo1,InterfaceDemo2
class ImplementsDemo implements InterfaceDemo1 implements InterfaceDemo2class ImplementsDemo extends Sample implements InterfaceDemo2

Polymorphism:
Polymorphism means one thing is in many ways. In JAVA we can achieve in two ways
1.      By using method overloading
Ex:
public class MethodOverLoadingDemo {
      // this is called overloading method
      public void methodOne(int i){
            System.out.println("I am MethodOverLoadingDemo methodOne()-->"+i);
      }
}
class MethodOverloadingImpl extends MethodOverLoadingDemo{
      public static void main(String[] args) {
            // calling child class method with child class reference with child class object
            MethodOverloadingImpl methodOverloadingImpl= new MethodOverloadingImpl();
            methodOverloadingImpl.methodOne();
            //calling parent class method with child class reference with child class object
            methodOverloadingImpl.methodOne(0);
            // calling child class method with parent class reference with child class object
            // In method Overloading method calls will be decides based on reference not on based on created object
            MethodOverLoadingDemo methodOverLoadingDemo = new MethodOverloadingImpl();
            methodOverLoadingDemo.methodOne(0);
            //methodOverLoadingDemo.methodOne(); -- this is not possible
      }
      // this is overloaded method
      public void methodOne() {
            System.out.println("I am MethodOverloadingImpl methodOne");

      }
}

Output:

I am MethodOverloadingImpl methodOne
I am MethodOverLoadingDemo methodOne()-->0
 I am MethodOverLoadingDemo methodOne()-->0

Important Points:
1.       This is called static polymorphism.
2.       All the method calls will decides based on references not based on objects
3.       Two methods are said to be overloaded if they have the same method names but different parameter list (At least order).
4.       We never consider return type, access modifier and throws clause in overloading.
5.       In method overloading, which method need to call decided by the compiler at compile time. So overloading is called “static polymorphism” OR “early binding”


2.      Method Overriding
Ex:
public class MethodOverridingDemo {
      //This is overriding method
      public int methodOne(){
            System.out.println("I am MethodOverridingDemo methodone()");
            return 10;
      }
}
class MethodOverridingImpl extends MethodOverridingDemo{
      //This is overridden method
      // In method Overriding method signature should be same
      public int methodOne() {
            System.out.println("I am MethodOverridingImpl methodOne()");
            // return value can be different data type which can be internally promoted to declared data type
            return 'a';
      }
      public static void main(String[] args) {
            // calling child class method with child class reference with child class object
            MethodOverridingImpl methOverridingImpl = new MethodOverridingImpl();
            methOverridingImpl.methodOne();
            // calling child class method with Parent class reference with child class object
            // In method Overriding method calls will be decides based on created object not based on reference
            MethodOverridingDemo methodOverridingDemo = new MethodOverridingImpl();
            methodOverridingDemo.methodOne();
           
      }
}

Output:

I am MethodOverridingImpl methodOne()
I am MethodOverridingImpl methodOne() 

Important Points:
1.       In overriding the method names and arguments (including order must be same).
2.       In case of overriding signatures of the methods must be same.
3.       Access modifier incremental is allowed in method overriding.
4.       An abstract method can be overridden as non-abstract method
5.        While overriding we can’t increase the no.of checked exceptions & we can‘t directly use parent exception.
6.       There is no rule for unchecked exceptions


Encapsulation:

  Encapsulation means bundling the data into a one unit. In JAVA we have two types of encapsulations
1.       Partial encapsulation : class is the best example for partial encapsulation because a call can contains all kinds of elements with all kinds of access modifiers
2.       Fully encapsulation: JAVA bean is the best example for full encapsulation because all the members are private and getters and setters are public access modifiers

         Ex: Bean example
public class JavaBeanDemo {

       private int sno;
       private String sname;
       public int getSno() {
              return sno;
       }
       public void setSno(int sno) {
              this.sno = sno;
       }
       public String getSname() {
              return sname;
       }
       public void setSname(String sname) {
              this.sname = sname;
       }
}

Important Points:
1.       All the members should be private
2.       Setters don’t have return type
3.       Setters have private access
4.       Getters have public access modifier
5.       Getters have return type of corresponding data member data type declared
6.       Getters have return type whereas setters don’t
7.       Setters have parameter whereas getter doesn’t

No comments:

Post a Comment