OOPS concepts Tutorial

OOPS concepts Tutorial

Summary: By the end of this tutorial "OOPS concepts Tutorial", you will understand the meaning of OOPS concepts.

Three basic concepts of OOPS

  1. Abstraction
  2. Encapsulation
  3. Polymorphism

1. Abstraction

Abstraction means using the equipment (or code) without knowing the details of working. For example, you are using your mobile phone without knowing how a mobile operates internally. Just a click on a button connects to your friend. This is abstraction. That is, the details of mobile mechanism are abstracted. Similarly we use the code of somebody to get the functionality without knowing how the code is written or working. For example, you are using printf() function to write to the DOS prompt without the awareness of what the code of printf(). One of the ways of achieving abstraction is inheritance. Through inheritance a subclass can use the super class methods as if they belong to it without caring their implementation details.

2. Encapsulation

Object-oriented concepts borrowed many terms from other technologies like encapsulation (from pharmaceuticals), inheritance (from biology), cloning (from genetics) and polymorphism (from biology) etc. Placing a powdered drug in a gelatin capsule and sealing it is known as encapsulation. With encapsulation, a Pharmacist hides the properties of a drug like its taste and color from the patient. Similar meaning is in OOPs also. Encapsulation hides the implementation details of coding; other way it is abstraction. With abstraction, implementation of information is hidden.

In a programming language, variables represent the properties and methods are used to change the properties. For example, the speed variable represent the property of a motor car and the method accelerator() is used to change the speed. Objects are used to call the methods. There may be multiple motor cars and every car has its own speed. Here, motor car represents an object. Every object encapsulates its own data. This encapsulation concept takes OOP languages a lead over traditional procedure-oriented languages. Binding data with objects (generally through method calls) is known as encapsulation.

In encapsulation, to have control over the manipulation of data (not to feed wrong data, for example, the speed cannot be negative) by other classes, a programmer declares variables as private and methods as public. Other classes can access the private variables through public methods. With encapsulation, every object maintains its own data and this data is entirely private to that object. Other objects cannot access or modify the data.

3. Polymorphism

Polymorphism is a Greek term and means many forms of the same ("poly" means many and "morphism" means forms). It is an OOP paradigm where one method can be made to give different outputs (functionalities) when called at different times. Polymorphism is two ways – static polymorphism where methods are binded at compile time and dynamic polymorphism where methods are binded dynamically at runtime. The same person is called as an officer (in office), husband (in house) and player (in cricket team). The person can be treated as base class. Extra subclasses can be added by hierarchical inheritance like son etc.

11 thoughts on “OOPS concepts Tutorial”

  1. mohan
    August 14, 2012 at 11:58 am
    public class Person
    {
    String name;
    int age;
    void talk()
    {
    System.out.println(“hello I am “+name);
    System.out.println(“hello my age “+age);
    }
    }
    class Demo125
    {
    public static void main(String[] args)
    {
    Person p=new Person();
    p.name=”mohan”;
    p.age=25;
    p.talk();
    System.out.println(“hashcode(reference) of object= “+p.hashCode());
    }
    }
    error:class Person is public ,should be declared in a file named Person.java
    public class Person
    ^
    plz rectify the problem why can’t i use public or protected there .thank you

    1. In real life, a woman is polymorphic. For a son, she is mother, for a husband she is wife, for a father she is daughter etc. The same woman does different jobs with different people. This we called as polymorphism. Same way, the same method can do different jobs like calculating triangle area, rectangle area etc,

  2. Sir I am a fresher Student with moderate level of knowledge in java and willing to join your prestigious java classes. please help me.

    I’ll thought my self if i ever get a chance to attend your class.

  3. Sir,
    encapsulation in other way is abstraction. With abstraction, implementation of information is hidden.
    I didn’t get this point clearly sir,
    Could you please explain a little bit more ?
    Thanks

  4. public class Person
    {
    String name;
    int age;
    void talk()
    {
    System.out.println(“hello I am “+name);
    System.out.println(“hello my age “+age);
    }
    }
    class Demo125
    {
    public static void main(String[] args)
    {
    Person p=new Person();
    p.name=”mohan”;
    p.age=25;
    p.talk();
    System.out.println(“hashcode(reference) of object= “+p.hashCode());
    }
    }
    error:class Person is public ,should be declared in a file named Person.java
    public class Person
    ^
    plz rectify the problem why can’t i use public or protected there .thank you

    1. Just do one simple thing…
      Create a separate file for both Person.java and Demo.java declare both class as public..
      or
      just change the access specifier of Person as default and Demo125 as Public and save the entire file with name Demo125.java.. Your program will definitely run…

Leave a Comment

Your email address will not be published.