2014-01-08

Introduction to Object Oriented Programming Concepts Java

Introduction to Object Oriented Programming Concepts Java

What is OOP?

OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything inOOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.
In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.

4.4. What is an Object?

An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address.
In pure OOP terms an object is an instance of a class.

4.5. What is a Class?

OOP_Concepts_and_manymore/class.gif
class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of anobject. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.
public class Student
{
}
According to the sample given below we can say that the Student object, named objectStudent, has been created out of the Student class.
Student objectStudent = new Student();
In real world, you'll often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles.
In the software world, though you may not have realized it, you have already used classes. For example, the TextBoxcontrol, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.

4.6. How to identify and design a Class?

This is an art; each designer uses different techniques to identify classes. However according to Object Oriented Design Principles, there are five principles that you must follow when design a class,
  • SRP - The Single Responsibility Principle -
    A class should have one, and only one, reason to change.
  • OCP - The Open Closed Principle -
    You should be able to extend a classes behavior, without modifying it.
  • LSP - The Liskov Substitution Principle-
    Derived classes must be substitutable for their base classes.
  • DIP - The Dependency Inversion Principle-
    Depend on abstractions, not on concretions.
  • ISP - The Interface Segregation Principle-
    Make fine grained interfaces that are client specific.
For more information on design principles, please refer to Object Mentor.
Additionally to identify a class correctly, you need to identify the full list of leaf level functions/ operations of the system (granular level use cases of the system). Then you can proceed to group each function to form classes (classes will group same types of functions/ operations). However a well defined class must be a meaningful grouping of a set of functions and should support the re-usability while increasing expandability/ maintainability of the overall system.
In software world the concept of dividing and conquering is always recommended, if you start analyzing a full system at the start, you will find it harder to manage. So the better approach is to identify the module of the system first and then dig deep in to each module separately to seek out classes.
A software system may consist of many classes. But in any case, when you have many, it needs to be managed. Think of a big organization, with its work force exceeding several thousand employees (let’s take one employee as a one class). In order to manage such a work force, you need to have proper management policies in place. Same technique can be applies to manage classes of your software system as well. In order to manage the classes of a software system, and to reduce the complexity, the system designers use several techniques, which can be grouped under four main concepts named Encapsulation, Abstraction, Inheritance, and Polymorphism. These concepts are the four main gods of OOP world and in software term, they are called four main Object Oriented Programming (OOP) Concepts.


Multi Level Inheritance

package oops;
public class Oops {
public static void main(String[] args) {
        salary em=new salary();
        em.insertData();
    }
 }
  class employee
    {
        public void insertEmployee()
        {
            System.out.println("Name: Suresh");
            System.out.println("Address:kathmandu");
           
        }
    }
class department extends employee
    {
        public void insertDepat()
        {
            insertEmployee();
            System.out.println("Department: IT");
            System.out.println("Level:L2");
           
        }
    }
class salary extends department
    {
        public void insertData()
        {
            insertDepat();
            System.out.println("Ta: 200");
            System.out.println("Da:500");
            System.out.println("Basic: 12000");
            System.out.println("Gross:12700");
        }
    }

Multiple Inheritance


package oops;
import java.lang.*;
import java.io.*;
public class Oops {
public static void main(String[] args) {
Result R = new Result("Ra.one",12,93,84);
                                R.display();
                                R.percent_cal();
    }
 }
interface Exam
{
                void percent_cal();
}

class Student
{
                String name;
                int roll_no,mark1,mark2;
                Student(String n, int r, int m1, int m2)
                {
                                name=n;
                                roll_no=r;
                                mark1=m1;
                                mark2=m2;
                }
                void display()
                {
                                System.out.println ("Name of Student: "+name);
                                System.out.println ("Roll No. of Student: "+roll_no);
                                System.out.println ("Marks of Subject 1: "+mark1);
                                System.out.println ("Marks of Subject 2: "+mark2);
                }
}
class Result extends Student implements Exam
{
                Result(String n, int r, int m1, int m2)
                {
                                super(n,r,m1,m2);
                }
                public void percent_cal()
                {
                                int total=(mark1+mark2);
                                float percent=total*100/200;
                                System.out.println ("Percentage: "+percent+"%");
                }
                void display()
                {
                                super.display();
                }
}


Polymorphism

package oops;
public class Oops {
public static void main(String[] args) {
      
    cal c=new cal();
    System.out.println("Calculation:"+c.add(10, 20));
    System.out.println("Calculation:"+c.add(10, 20));
    }
 }
  class cal
    {
        public int add(int x,int y)
        {
            return x+y;
        }
        public int add(int x,int y,int z)
        {
            return x+y+y;
           
        }
    }




Encapsulate

package oops;
import java.lang.*;
import java.io.*;
public class Oops {
public static void main(String[] args) {
       EncapTest encap = new EncapTest();
      encap.setName("James");
      encap.setAge(20);
      encap.setIdNum("12343ms");
      System.out.print("Name : " + encap.getName()+
                             " Age : "+ encap.getAge());
    }
}
 class EncapTest{

   private String name;
   private String idNum;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public String getIdNum(){
      return idNum;
   }

   public void setAge( int newAge){
      age = newAge;
   }

   public void setName(String newName){
      name = newName;
   }

   public void setIdNum( String newId){
      idNum = newId;
   }
}


Abstract Class

package oops;
import java.lang.*;
import java.io.*;
public class Oops {
public static void main(String[] args) {
      Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
      Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);

      System.out.println("Call mailCheck using Salary reference --");
      s.mailCheck();

      System.out.println("\n Call mailCheck using Employee reference--");
      e.mailCheck();
    }
}
 /* File name : Employee.java */
 abstract class Employee
{
   private String name;
   private String address;
   private int number;
   public Employee(String name, String address, int number)
   {
      System.out.println("Constructing an Employee");
      this.name = name;
      this.address = address;
      this.number = number;
   }
   public double computePay()
   {
     System.out.println("Inside Employee computePay");
     return 0.0;
   }
   public void mailCheck()
   {
      System.out.println("Mailing a check to " + this.name
       + " " + this.address);
   }
   public String toString()
   {
      return name + " " + address + " " + number;
   }
   public String getName()
   {
      return name;
   }
   public String getAddress()
   {
      return address;
   }
   public void setAddress(String newAddress)
   {
      address = newAddress;
   }
   public int getNumber()
   {
     return number;
   }
}

/* File name : Salary.java */
class Salary extends Employee
{
   private double salary; //Annual salary
   public Salary(String name, String address, int number, double
      salary)
   {
       super(name, address, number);
       setSalary(salary);
   }
   public void mailCheck()
   {
       System.out.println("Within mailCheck of Salary class ");
       System.out.println("Mailing check to " + getName()
       + " with salary " + salary);
   }
   public double getSalary()
   {
       return salary;
   }
   public void setSalary(double newSalary)
   {
       if(newSalary >= 0.0)
       {
          salary = newSalary;
       }
   }
   public double computePay()
   {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
}





Abstract Method

package oops;
import java.lang.*;
import java.io.*;
public class Oops
 {
public static void main(String[] args)
 {
  Salary s=new Salary();
  s.computePay();
}
}
  abstract class Employee
{
   private String name="Suresh";
   private String address;
   private int number;
   public abstract double computePay();
    public String getName()
   {
      return name;
   }
}

class Salary extends Employee
{
   private double salary; // Annual salary
   public double computePay()
   {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }

}

 


 

No comments:

Post a Comment