Friday, 6 November 2015

Concepts of Object Oriented Programming


  • OOP stands for Object Oriented Programming.
  • A programming methodology based on Objects, instead of just functions and procedures. An “object” in OOP language refer to a specific type of a class. Each object has a structured similar to other objects in the class, but can be assigned individual characteristics.
  • An Object can also called as Functions or Methods, Specific to that object.
  • OOP makes it easier for programmers to structure and organize software programmers to structure and organize software programs.
  • Object Oriented Programming is a design philosophy.

 OOP’s have Following Features:
  1. Object - Instance of class.
  2. Class – Blue print of Object.
  3. Inheritance – One property of object is acquiring to another property of object.
  4. Encapsulation – Protecting our data.
  5. Polymorphism – Different behaviors at different Instances.
  6. Abstraction – Hiding our irrelevance data.
Object Oriented Programming is a programming method that combines:
  1. Data
  2. Instructions for processing that data into a self-sufficient “object” that can be used within a program or in other programs.
Programming Techniques
  1. Unstructured Programming- (Assembly language programming)
  2. Procedural Programming- (Assembly language, C programming)
  3. Object Oriented Programming- (C++, Java, Smalltalk, C#, Objective C)
What is Object?
  • A bundle of related variables and functions, And object share two characteristics they are:
    • State – A well-defined condition of an item, A state captures the relevant aspects of an object.
    •  Behavior – The observable effects of an operation or event.
An Example for Object, State and Behavior:


  • Object: House
  • State: Current Location, Area of House, Color of House etc…
  • Behavior: Close or Open main door.
Class
  • A Class is a prototype that defines the variables and the methods common to all object of a certain kind.
  • Member functions operate upon the member variables of the class.
  • An object is created when a class in instantiated.
How to create an object?
  • An object is created when a class is instantiated.
Declaring an object of class:

“Class Name” “Object Name”;

Constructor
  • A method with the same name as class name used for the purpose of creating an object in a valid state.
  • It does not return a value not even void.
  • It may or may not have parameters.
  • A class contains one or more constructors for making new objects of the class.
  • If the programmer does not write a constructor, Java provides a default constructor with no arguments.
Object Oriented Programming Features:
  1. Inheritance
  2. Polymorphism
  3. Encapsulation
  4. Abstraction

Inheritance

It provides the idea of reusability of code and each sub class defines only those features that are unique to it.
  • Inheritance is a mechanism of defining a new class based on an existing class.
  • Inheritance enables reuse of code.
  • Inheritance helps in specialization.
  • The existing class is called as base class, and new class which inhnherits from the base class is called the sub class.
  • Inheritance implements the “Is-A” relationship.
  • And the advantage of inheritance is that code in base class need not be rewritten in the sub class, the member variables and the methods of the base class can be used in the sub class as well.
Example:

01public class Animal {
02    public Animal() {
03        System.out.println("A new animal has been created!");
04    }
05     
06    public void sleep() {
07        System.out.println("An animal sleeps...");
08    }
09     
10    public void eat() {
11        System.out.println("An animal eats...");
12    }
13}

A Real Time Example For Inheritance: - Relationship


  • Father and Son Relationship
Types of Inheritance:
  • Multi-level inheritance - It refers to a mechanism in OO technology where one can inherit from a sub class, thereby making this sub class the base class for the new class.
  • Multiple inheritance – The concept of 1 class inheriting from more than 1 base class. The problem with multiple inheritance is that the sub class will have to manage the dependency on two base classes, and it very rarely used in software projects. Multiple inheritance results in unwanted complexity when further extending the class.
  • Multi-level inheritance” is allowed in Java but “multiple inheritance” not allowed in Java.The new OO Languages like small talk, Java, C# do not support multiple inheritance, and the multiple inheritance is supported in C++.

Encapsulation

It mean the localization of the information within an object, and is also called as “Information Hiding”.
  • Objects encapsulate data and implementation details.
  • An object exposes its behavior by means of methods or functions.
  • The set of functions an object exposes to other objects.
  • Writing operations and methods stored in a single class
Advantage of Encapsulation:
  • The functionality where we can change the implementation code without breaking the code of others who use our code is the biggest benefit of encapsulation.
  • We can rework on our method code at a later point of time.
A Real Time Example For Encapsulation: - Medical Capsuals


  • One drug is stored in button layer and another drug is stored in upper layer these two layers are combined in single capsual.

Polymorphism

  • It is a feature that allows one interface to be used for a general class of actions. It’s an operation may exhibit different instances.
  • The behavior depends on the types of data used in the operation.
  • It plays an important role in allowing objects having different internal structures to share the same external interfaces.
  • It is extensively used in implementing inheritance.
A Real Time Example For Polymorphism:Person


  • Person in Home act is Son, In office acts Employer, In public Good Citizen.

Types of Polymorphism

  1. Static Polymorphism: Same class more than one method having same name but differing in signature, Resolving during compilation time, Return type is not part of method signature.
  2. Dynamic Polymorphism: Keeping the signature and return type same, method in the base class is redefined in the sub class, Resolved during run time, the method to be invoked is decided by the object that the reference points to and not by the type of the reference.

Abstraction

  • It hide the information that is not relevant, the process of forming of general and relevant concept from more complex scenarios.
  • Abstraction is used to build complex systems.
  • It is not a feature of OO Concepts alone.
  • In Procedural language programming, abstraction can be achieved to a limited extent by hiding complex internals through well-formed business logic and functions.
A Real Time Example For Abstraction: - TV Remote Button


  • That number format and power buttons and other buttons there, Just we are seeing the buttons and we don't see the button circuits and wiring's all are hidden.
What is meant by method overloading in Java?
  • Feature found in various programming languages such as Ada, C++, C#, Delphi, D, Java and Swift that allows creating several methods with the same name which differ from each other in the type of the input and the output of the Function.
What is meant by method overriding in Java?
  • The ability of a subclass to override a method allows a class to inherit from a Super class whose behavior is close enough and then to modify behaviour as needed.
  • The Overriding method has the same name, number and type of parameters, and return type as the method that it overrides.
Difference between Overloading and Overriding in Java Programming.



Summary
  • An object is an instance of a class
  • Encapsulation provides the security that keeps data and methods safe from inadvertent changes
  • Inheritance is parent-child relationship of class which is mainly used for code reusability
  • Polymorphism definition is that poly means Many and morphos means forms.
  • Using abstraction one can simulate real world objects
  • Abstraction provides advantage of code reuse
  • Abstraction enables program open for extension.



8 comments:

  1. really good work...keep it up

    ReplyDelete
  2. really good work...keep it up

    ReplyDelete
  3. this blog gives me good knowledge about java concepts. everyone can understand very easily. all the best brother.keep it up.

    ReplyDelete
  4. Good work,keep it up this one is simple to understand.

    ReplyDelete
  5. Interesting..it was useful 👌

    ReplyDelete
  6. Interesting..it was useful 👌

    ReplyDelete
  7. It's really help to understand about Java.... Good job

    ReplyDelete