You are here

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.It means focussing only on the essential detail and overlooking the non-essential detail of an object.
  • 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 handleas 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 subbclass 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:
syntax:

 class subclassname extends superclassname
{
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("John Smith");
      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.

Application OF OOP:

  • Real-time system
  • Simulation and modelling
  • Object-oriented datbase
  • Hypertext,hypermedia and expertext
  • AI and expert system
  • Neural network and parallel progarmming
  • Decision support and office automation system
  • CIM/CID/CAD system

 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.

Comments

Can you tell me what is the purpose of using multilevel and hierarchical inheritance in java? And why I use them when to use them with example??????????????

I want to know how to show polymorphism concept in interactive manner?

Add new comment