Tuesday 2 April 2013

Core Java Tutorial OOPS Concepts with Examples



Core Java Tutorial OOPS Concepts with Examples

There are four main features of OOPS.
1) Encapsulation                                 

2) Inheritance              
3) Polymorphism                     
4) Abstraction

Encapsulation: Encapsulation means putting together all the variables (Objects) and the methods into a single unit called Class. So define a user defined data type called the keyword Class followed by the class name and inside the class we need the specify the variables that is also called as attributes and methods.


Example :


Class 
EncapsulationExample{
              int iVariable;
              string sVariable;
              public void javaMethod(){   }
}


Inheritance : (Code re-usability)

You already have defined an Object or rather you have already defined set of attributes and characteristics which you like to make you it again and expand up on it. So you are making use of already written class and further extending on that. That why we discussed about the code re-usability the concept. In general one line definition we can tell that deriving a new class from existing class, it’s called as Inheritance. You can look into the following example for inheritance concept.

Example 1:
package test;

class employeeDetails {

              String name;
              int employeeID;
              
              int get(String n, int id){
             name=n; 
             employeeID=id; 
             return(0);
              }
              
              void showDetails(){
              System.out.println("Name : " + name);
              }
}

class InheritanceExample extends employeeDetails{

              public static void main(String args[]){
             employeeDetails employeeObject = new employeeDetails();
             employeeObject.get("Anand",1234);
             employeeObject.showDetails();
              }
              
              void displayDetails(){
             System.out.println("Employee Info Display");
              }
}


Output:
Name : Anand



Polymorphism: 

In Core Java Polymorphism is one of easy concept to understand. Polymorphism definition is that Poly  means Many morphos means froms . Its refer to the objects ability to active Polymorphism depends on its type.

There are two types of Polymorphism available in Java.

1) Static Polymorphism    -  Method Overloading                               
2) Dynamic Polymorphism - Method overridding

Static Polymorphism, Its Compile time Polymorphism. Method overloading is the concept of two or more methods in a Java Class can have same name and it their arguments lists are different. 


We also have another two important concepts in Polymorphism, 

* Method Overloading
* Method Overridding

The following example program will make you understand the Method Overloading.


Example for Method overloading


package test;


class addNumbers {

              void add(int number1, int number2){
             System.out.println("Addition of 2 numbers " + number1 + number2);
              }
              void add(int number1,int number2,int number3){
              System.out.println("Addition of 3 numbers  "+ number1+number2+number3);
              }
}
public class MethodOverloadingExample{

public static void main(String arg[]){
addNumbers addNumObj =new addNumbers();

addNumObj.add(10, 20);   // add() method by passing 2 values

// Output: Addition of 2 numbers  30

addNumObj.add(10,20,30);   // add() method by passing 3 values

// Output: Addition of 3 numbers  60
}
}


Output for the above program is :
Addition of 2 numbers  30
Addition of 3 numbers  60


Dynamic polymorphism, Its run time polymorphism. We can also call it as Method overridding. 

Method overridding is the concept of two or more method, constructor have a same name in super and sub class with same signature. This feature is called method overridding.

Method Overloading Example Program:

class doOnlyAddition {
              void calculate(int number1, int number2){
             System.out.println("Addition is  "+ number1 + number2 );
             }
}
class doAllCalulation extends doOnlyAddition {
void calculate(int number1, int number2){
System.out.println("Addition is  " + (number1 + number2) );
System.out.println("Subtraction is  " + (number1 - number2) );
System.out.println("Multiplication is  " +  (number1 * number2) );
System.out.println("Division is  " + (number1 / number2) );
}

public static class MethodOverriddingExample{
              public static void main(String arg[]){
             doAllCalulation calAll=new doAllCalulation();
             calAll.calculate(8,4); 
             doOnlyAddition calAdd=new doOnlyAddition();
             calAdd.calculate(4,2 );
              }
}

Output for the above program is :Addition is  12
Subtraction is 4 

Multiplication is 32

Division is  2
Addition is  6

Abstraction : ( Hiding irrelevant details and Provide only required data as per access role)

Process of exploring relevant details and hiding irrelevant details this feature is known as Abstraction. In other way making simplicity to use complex system. One does not want to understand how to engine works. Similarly one does not have to understand the internal implementation of the software objects.  Abstraction Example : Engine, Driving.
Abstraction main advantage is that every user will get data according to their exact requirement. User will not get confused with unnecessary data. The following example program will make you to understand Abstraction.

Java Abstraction Example Program:


public class AbstractionExample {

private int accountNo;
private String customerName;
private float accountBalance;
private float profit;
private float loan;


public void dislayClerkInfo(){
          System.out.println(“Accout number “+accountNo);
          System.out.println(“Customer name “+customerName);
          System.out.println(“Account Balance “+accountBalance);
}

}

In the above program clerk can access only the limited details like account number, customer name and account balance. User cannot access other details like profit and loan.