• Properties of object oriented programming(OOP)


    Properties of object oriented programming

     An object oriented programming language provide mechanism that help us implement object oriented model. The three features of object oriented programming are –

    Encapsulation:

    (Encapsulation is the mechanism that binds together code and data it manipulates, and keeps both safe from outside interface and misuse). One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by the other code defined outside this wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. In programming, power of encapsulated code is that everyone knows how to access it and thus can use it. Regardless of implementation details.
    (In java the basics of encapsulation is the class) A Class defines the structure and behaviour that will be shared by a set of objects. Each object of a given class contains the structure and behaviour defined by the class as if it were stamped out by a mould in shape of a class. For this reason, objects are sometimes referred to as instance of a class.

    class A
    {
    int a;
    void incre ()
    {
    a =10;
    a++;
    }
    }
    In this code, member variable a and member method incre is encapsulated in class A and everyone can use incre without knowing its implementation.

    Inheritance:

    (Inheritance is the process by which one object acquires properties of another object. This is important because it supports the concept of hierarchical classification.) Using inheritance, we can create a general class that defines traits common to a set of relative items. This class can be inherited by other more specific classes each adding those things that are unique to it.( A class that is inherited is called a super class. The class that does the inheriting is called a sub class.) Therefore, a sub class is specialized version of super class. It inherits all of the members defined by super class and add its own unique element. 

    class A

    {
    int i,j;
    void showij()
    {
    System.out.println(“i : ”+i+”j : ”+j);
    }
    }
    class B extends A
    {
    int k;
    void showk()
    {
    System.out.println(“k : ”+k);
    }
    void sum()
    {
    System.out.println(“Sum : “+(i+j+k));
    }
    }
    In above code, we can see that to inherit a class we simply incorporate the definition of one class into another class by using the extends keyword. We have created a superclass called A and a sub class B that has extended A.

    Polymorphism:

     It is a feature that allows one interface to be used for a general class of actions. The specific action i.e. determined by the exact nature of the situation. More generally the concept of polymorphism is often expressed by the phrase ‘One interface multiple methods’. This means that it is possible to design a generic interface to a group of related activities. This helps to reduce the complexity by allowing the same interface to specify a general class of action. Java supports polymorphism in various different ways. 
    1. One of the way is method overloading.

    class A
    {
    void show ()
    {
    system.out.print ();
    }
    void show (int a)
    {
    system.out.print (“a=” +a);
    }
    }

    In the above code, show method is overloaded. As we can see there are two versions of show method, whenever an overloaded method is invoked java uses the type or number of arguments as its guide to determine which version of the overloaded method to actually all. Thus the overloaded method must differ in types or number of parameters although they have same name.

    2. Another way in which java allows a programmer to fully utilize the “one interface, multiple methods” aspect of polymorphism is through interface. Interface allows a programmer to fully abstract a class. Interface has their methods declared without any body, whichever class implements the interface must provide the complete set of methods required programmer to achieve polymorphism.
    Interface call
    {
    Void called (int a)
    }
    This interface can be implemented by any number of classes and all the classes are allowed to give unique definitions to the methods of interface. Thus one method can have multiple definitions which is the essence of polymorphism.  

    3. Java allows a programmer to achieve run time polymorphism by using the concept of dynamic method dispatch. A super class reference variable can refer to a sub class object. Java uses this fact to resolve calls to overridden method. An overridden method is a method in the subclass that have the same name and type signature as method in its super class. Java determines which version of the method to execute based upon the type of the object being referred to at the time of the call occurs.

    Example:
    class A
    {
    void call()
    {
    system.out.print(“Inside Class A”);
    }
    }
    class B extends A
    {
    void call()
    {
    System.out.print(“Inside Class B”);
    }
    }
    class Dispatch
    {
    public static void main(String []s)
    {
    A a=new A();
    B b=new B();
    A x;
    x.call();
    x=b;
    x.call();
    } 
    }

  • You might also like

    No comments:

    Post a Comment

search topics

NEWSLETTER

Get All The Latest Updates Delivered Straight Into Your Inbox For Free!