Saturday, August 16, 2014

Java Object Oriented Concepts

Object-Oriented Programming concept

Object-oriented Programming is designed to model the real world concept into a computer program.It considers real world things as object.Ex. Bicycle,car,book and so on. This is an approach that provides a way of modularizing program by creating partitioned memory area for both data and function that can be used as templates for creating copies of such modules on demand.

The OOP Principles
  • Abstraction - It deal with the complexity of an object and how that complexity is represented. It's also a mechanism to represent real world concepts of software world. Think of it as a model that is created to represent the problem domain concepts. For example many times we create mental models to understand certain things and that helps in comprehension. In the same way, in the context of software, abstraction is a way of representing problem domain. For example, take School Management System. To make such a system, we need to map many concepts from real world and map it to Software world. The concept of Student and Teacher, which are straightforward to understand and map. Now let's see the concept of pass and fail. How to represent that as there might be many rules. The rules could be

- Minimum marks in individual subjects
- How many subjects one need to have certain marks to pass.
- Different class might have different criteria
- Many more
How to represent all the concepts in software world. That's where abstraction comes in picture. It's about capturing all the details in certain class structure. It's also about focusing on the essential and relevant details and ignore the non-essential details.

  • Encapsulation:  Wrapping up of data and method into a single unit is known as encapsulation. e.g.  capsule,  from the outside its just a cap, and it hides everything that is contained within. But what lies inside may be 2 or 3 or more powders loosely arranged and packed within.An object is something similar. It is created with the immense power of a class. while the composition of the class could be anything (as compared to the capsule), one may not know what is contained when you create the object handles in

A obj = new A();  

you may say obj here is like the capsule to all those who want to consume it in their programs. So, with this object one can use its inherent power. Thus this concept of hiding away its true power is known as Encapsulation.

  • Inheritance:The mechanism of deriving a new class from an old one is called inheritance.The old class is known as base class or super class or parent class and new class is called the subclass or derived class or child class.The concept of inheritance provides the idea of reusability.This means that we can add additional features to an existing class without modifying it.Different Types of Inheritance

Single inheritance(only one super class)
Multiple inheritance(several super classes)
Hierarchical inheritance(one super class, many subclasses)
Multilevel inheritance(Derived from a derived class) note:java does not directly implement multiple inheritance,it is implemented using secondary inheritance path in the form of interface.

Defining a subclass:

class SubClass extends SuperClass
{
varialbe declaration;
method declaration;
}

The keyword extends is used to inherit the properties of base class to derived class.
Ex.


class User
{
   private String name;
   public void setName(String n)
   {
      name = n;
   }
   public String getName()
   {
      return name;
   }
}

class Student extends User
{
   private String stuNum;
   public void setStuNum(String sn)
   {
      stuNum = sn;
   }
   public String getStuNum()
   {
      return stuNum;
   }

Finally we have to create a program that will instantiate a Student object, set the name and student number and then print the values using the get methods.


public class TestInheritance
{
   public static void main(String[] args)
   {
      Student stu = new Student();
      stu.setName("Ishana Bhatt");
      stu.setStuNum("12345");
      System.out.println("Student Name: " + stu.getName());
      System.out.println("Student Number: " + stu.getStuNum());
   }
}

Polymorphism:It means "one interface,multiple methods".The ability to take  more than one form.It means to design a generic interface to a group related activities.It is compiler's job to select the specific action as it applies to each situation.EX. An operation show different behaviour in different instance.consider the operation of addition,for 2 no,the operation will generate sum.If the operands are string ,then the operation would produce a third string by concatenation.

Benefits Of OOP

  • Through inheritance, we can eliminate redundant code and extend the use of existing classes.
  • We can build secure  programs using data hiding concept.
  • It is easy to parition the work in project based on object.
  • Software complexity can be easily managed.
  • More on Java

No comments:

Post a Comment