OOPs concepts 1

Object-oriented programming (OOP) is a programming paradigm that represents concepts as “objects” that have data fields (attributes that describe the object) and associated procedures known as methods.
 
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.
 
What is a Class?
A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.
 
for example:
public class Student
{
 
}
 
According to the sample given below we can say that the student object, named objectStudent, has created out of the Student class.
 
Student objectStudent = new Student();
 
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.
 
What is Encapsulation (or information hiding)?

The encapsulation is the inclusion within a program object of all the resources need for the object to function – basically, the methods and the data.oops1

In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system. That idea of encapsulation is to hide how a class does it but to allow requesting what to do.

 
 
 
What is Abstraction?
Abstraction is an emphasis on the idea, qualities and properties rather than the particulars (a suppression of detail). The importance of abstraction is derived from its ability to hide irrelevant details and from the use of names to reference objects. Abstraction is essential in the construction of programs. It places the emphasis on what an object is or does rather than how it is represented or how it works. Thus, it is the primary means of managing complexity in large programs.
 
The abstraction can be used/applied in various situations/purposes: 
data abstraction, operational abstraction, functional abstraction, procedural abstraction, behavioral abstraction etc.
 
1. ADT’s (Abstract Data Types) or structures (struct) in C/C++, classes in C++/Java are Examples for data abstraction 
2. operators are examples for operational abstraction 
3. functions are examples for functional abstraction 
4. procedures in Pascal/FORTRAN/BASIC/COBOL are examples for procedural abstraction 
5. classes in C++/Java are examples for behavioral abstraction(Here, we can say, both data abstraction and functional abstraction are combined, and of course, called encapsulation.) 
 
for example:
prod  = 5 * 6;
 
here the operator * provides abstraction for multiplication operation, means the programmer/user can understand that the operator * will perform multiplication operation on 5 and 6 and then returns the product 30 to prod. 
But it will not say how this multiplication takes place. The underlying Language processor (compiler/interpreter) will take care how to multiply numbers
 
Depending on the complexity of the situation (problem/program/application/etc.,) the abstraction is achieved at several levels.
 
What is Inheritance?
Ability of a new class to be created, from an existing class by extending it, is called inheritance.
As the name inheritance suggests an object is able to inherit characteristics from another object. In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work the objects need to have characteristics in common with each other.
 
for example
public class Exception
{
}
 
public class IOException extends Exception
{
 
According to the above example the new class (IOException), which is called the derived class or subclass, inherits the members of an existing class (Exception), which is called the base class or super-class. The class IOException can extend the functionality of the class Exception by adding new types and methods and by overriding existing ones.
 
 
What is Polymorphism?
Polymorphisms is a generic term that means ‘many shapes’. More precisely Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.
In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading and method overriding,
 
What is Method Overloading?
The method overloading is the ability to define several methods all with the same name.
public class MyLogger
{
    public void LogError(Exception e)
    {
        // Implementation goes here
    }
 
    public bool LogError(Exception e, String message)
    {
        // Implementation goes here
    }
}
 
What is Operator Overloading?
The operator overloading (less commonly known as ad-hoc polymorphisms) is a specific case of polymorphisms in which some or all of operators like +, – or == are treated as polymorphic functions and as such have different behaviors depending on the types of its arguments
public class Complex
{
    private int real;
    public int Real
    { get { return real; } }
 
    private int imaginary;
    public int Imaginary
    { get { return imaginary; } }
 
    public Complex(int real, int imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }
 
    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
    }
}
 
What is Method Overriding?
Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes.
A subclass can give its own definition of methods but need to have the same signature as the method in its super-class. This means that when overriding a method the subclass’s method has to have the same name and parameter list as the super-class’s overridden method.
 
public class Animal
{
public void run(int legs)
{
// Implementation goes here
}
}
 
public class Dog extends Animal
{
public void run(int legs)
{
//implementation goes here
}
}

source: codeproject

Rate this post

One comment on “OOPs concepts

  1. Reply panic attack Jun 21,2014 11:13 am

    Thanks very interesting blog!

Leave a Reply