Saturday 24 December 2016

OOPS concepts in Java

OOPS concepts in Java



OOPS concepts in Java:


Object oriented programming  is a methodology in which programs are created by using classes and Objects.

Principles/Features

·       Abstraction
         Encapsulation
         Inheritance
         Polymorphism

 
Abstraction:
Mechanism of hiding the implementation complexity. Key is to simplify a complex design into smaller parts.

Abstraction in java can be achieved using Abstract classes. Read Abstract class in java

Encapsulation:

Mechanism of binding both data members and member methods in the same class.

Encapsulation in java can b achieved using Abstract classes and Interfaces. Read Interface in java

Uses:

Provides Security:

In tightly encapsulated classes data members make private. Hence, it can be accessed by other methods in the same class guaranty security.

Improved maintainability:

As all the methods while use the data members of class are included in the same class it is easy to find required method.

Example:

            package com.javasourceworld.oops;

            public class Encapsulation {
      
                   private int k=10; //binding the member variables as privaate
      
                   void show(){
                    //do something
                   }


              }


Inheritance:

Mechanism of acquiring some or all the features of one class into another class.

Java doesn’t support multiple inheritance to avoid complexity and ambiguity.(Diamond problem)

Example:

       package com.javasourceworld.oops;

             class Parent{
                   int x;
                   void show(){
                       //do something
                   }
             }

//Parent ->  Parent class/Base class/Derived class
//Child -> child class/sub class/Deriving class

        class Child extends Parent{
                int x;
                void doSomething(){
                 int y;
                }

             }

Benefits
·         Re usability
·         Improved maintainability
·         Minimized code redundancy
·         High productivity
·         Cost effective
·         Quality can be guaranteed
·         Errors can be avoided


Polymorphism:

Polymorphism is a Oops feature according to which a single entity can take different forms.

Example: ‘Shape’ that is square, circle. ‘Man’-son, father.

Types of Polymorphism

1.       Static Polymorphism.
                  Example: Overloading.

2.       Dynamic Polymorphism.
                  Example: Overriding.

When Polymorphism takes place at compile time it is called static Polymorphism.

When Polymorphism takes place at run time it is called Dynamic or runtime Polymorphism.


Overloading:

Signature : Signature of a method considers,

·         Number of parameters.
·         Datatype of parameters.
·         Order of parameters.

If the combination of these three values is unique then signature is unique.

Overloading:

Mechanism by which we can define more than one method with same name but different signature either in parent class or child class also.

Conditions for overloading

1.       Method name should be same.

2.       Methods return type is not considered.

3.       Signature must be unique.

4.       Access specifiers are also not considered.

5.       Overloaded method could static or non- static.

6.       ‘Final’ method could be overloaded.

7.       A single method could be overloaded by multiple overloading methods.

8.       The overloaded method to be executed it decided by compiler at the time of compilation itself.           (based on unique signature).

Overloading provides flexibility and simplifier the use of methods.

Constructors also can be overloaded.

* main method also can be overloaded but JVM calls only standard main method.

         package com.javasourceworld.oops;

         public class Bank {
             //Constructor overloading
           Bank(){
              //default constructor
           }
           Bank(int x){
              //Parameterized constructor
           }
          Bank(int x, double y){
              //Parameterized constructor
           }
      
          //Method overloading
          int calculateTax(int x){
              //do something
              return x;
          }
        
         double calculateTax(double x){
              //do something
              return x;
         }

      }

Overriding:

Mechanism by which we can define a method in child class whose name, signature and return type are same as the parent class, but implementation could be different.
Uses of overriding

·         In inherited method, implementation doesn’t suit the child’s requirement, the child can hide it by providing overriding method with suitable implementation.
Thus allows the child class to reuse useful features of parent class and hide unsuitable features.

·         It supports dynamic method dispatch, which can be used to reduce burden on the primary memory.

          package com.javasourceworld.oops;

          public class Account {
 
              void calculateTax(double x){
                     System.out.println("Calculating tax");
              }
            }

         class SavingAccount extends Account{
              void calcualteTax(double x){
                     System.out.println("Calculating Saving account tax");
              }
            }

         class FixedAccount extends Account{
             void calculateTax(double x){
                System.out.println("Calculating Fixed Account tax");
             }

         }


Conditions for overriding:

1. Method name and signature should be same as the parent class method.

2. Method return type should be either same or it could be co-variant return type.

3. The access specifier overriding method should be either same or wider than overridden method.

4. Overriding method cannot throw more checked exception.

5. Overriding method can throw unchecked exception, even though overridden method doesn’t throw.

6. Overridden method can have only one overriding method in a child class.

7. Only inherited method can be overridden.

8. are not inherited hence they cannot be overridden.

9. Private methods are not     so they can’t be overridden.

10. ‘Final’ methods cannot be overridden.

11. If the class itself is made ‘final’ we cannot create child class, cannot overridden.

12.  Static methods cannot overridden by non-static methods and vice-versa is also true.

13. Static method can be overridden by another static method. But we will not get benefit of dynamic Polymorphism.

Continue reading:
Association, Aggregation, Composition

No comments:

Post a Comment