Features Of OOPS Concepts

What is OOPS

  • Break down requirement into objects.
  • Built object hierarchies and interaction rather than program control flow.
  • Easy to change the code according to the requirements.

Feature of OOPS

  • Emphasis on data rather procedure.
  • Programs are divided into units called “Objects”.
  • Objects used to communicate with each other through functions.
  • New functionality can be easily developed by creating objects and functions.

OOPS Concepts

  • Classes and Object
  • Abstraction
  • Encapsulation
  • Inheritance and Interface
  • Polymorphism
  • Association, Aggregation and Composition
  • Coupling and Cohesion

Classes and Objects

  • OOP uses Objects.
  • To create an object inside program we must provide a definition for object – according to how they behave and what kind of information they contains – called “Class”.
  • Object” is a thing, both tangible and intangible.
  • Objects use] to communicate with each other through functions.
  • New functionality can be easily developed by creating objects and functions.

Abstraction

Abstraction is one of the principles of object oriented programming. It is used to display only necessary and essential features of an object to the outside world.

In simple words: Display what is necessary. Let other things rest in peace.

Abstract Base Class: There are some important points about Abstract Base Class. An Abstract Base class cannot be instantiated; it means the object of that class cannot be created.

  • Class having abstract keyword and having, abstract keyword with some of its methods (not all) is known as an Abstract Base Class.

  • Class having abstract keyword and having abstract keyword with all of its methods is known as pure Abstract Base Class.

  • The method of abstract class that has no implementation is known as "operation". It can be defined as abstract void method ();

  • An abstract class holds the methods but the actual implementation of those methods is made in derived class.

Abstraction Implementation

  1. class program  
  2. {  
  3.     abstract class animal  
  4.     {  
  5.         public abstract void eat();  
  6.         public void sound()  
  7.         {  
  8.             Console.WriteLine("dog can sound");  
  9.         }  
  10.     }  
  11.     class dog: animal  
  12.     {  
  13.         public override void eat()  
  14.         {  
  15.             Console.WriteLine("dog can eat");  
  16.         }  
  17.     }  
  18.     static void Main(string[] args)  
  19.     {  
  20.         dog mydog = new dog();  
  21.         animal thePet = mydog;  
  22.         thePet.eat();  
  23.         mydog.sound();  
  24.     }  
  25. }  
Encapsulation

Definition:
Encapsulation is hiding the functional data from the object calling it. Integration of data and operations (functions) in a class is Encapsulation.

Examples:
  1. Person from Driver: Can u drive the car?
    Driver: Yes, I can.
    Person: So tell me how does acceleration works.
    Driver: --------HUH?
    It means, details are encapsulated from the Driver.

  2. Doctor used to give us capsule … for ill patients only.

Type member access modifiers control what code has access to a specified type member.

access modifier

Inheritance and Interface

Definition:
Is a mechanism in OOP to design two or more entities that are different but share many common features,

  1. Feature common to all the classes are defined in Super class (Base Class).
  2. Classes that inherit common features from the super class is a “Derived Class”.

Why Inheritance:

  1. Improve maintainability.
  2. Reduce Cost.
  3. Avoid re-code and supports reusability.

Interface

Definition:
Group of related function whose usage is defined but not implementation.

An Interface definition specifies the interface’s member function called Methods, their return type, the number and type of parameters.
There is no implementation associated with the Interface.

Conclusion

An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations because they are inherited by classes and struts, which must provide an implementation for each interface member declared.

Interface Definition

  1. interface IMyInterface  
  2. {  
  3.     void MethodToImplement();  
  4. }  
  5. Interface Example: IDisposable interface  
  6. Interface Implementation  
  7. class InterfaceImplementer: IMyInterface  
  8. {  
  9.     static void Main()  
  10.     {  
  11.         InterfaceImplementer iImp = new InterfaceImplementer();  
  12.         iImp.MethodToImplement();  
  13.     }  
  14.     public void MethodToImplement()  
  15.     {  
  16.         Console.WriteLine("MethodToImplement() called.");  
  17.     }  
  18. }  
Polymorphism

Polymorphism means many forms. When a message can be processed in different ways it is called polymorphism.

Polymorphism provides the following features:
  • It allows you to invoke methods of derived class through base class reference during runtime or compile time.
  • It has the ability for classes to provide different implementations of methods that are called through the same name.

Polymorphism is of two types:

  1. Compile Time Polymorphism/Overloading
  2. Runtime Polymorphism/Overriding

Compile Time Polymorphism

Compile time polymorphism is method and operator overloading. It is also called early binding. In method overloading method performs different task at the different input parameters.

Runtime Polymorphism

Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

Practical example of Method Overloading (Compile Time Polymorphism)

  1. using System;  
  2. namespace method_overloading  
  3. {  
  4.     class Program  
  5.     {  
  6.         public class Print  
  7.         {  
  8.             public void display(string name)  
  9.             {  
  10.                 Console.WriteLine("Your name is : " + name);  
  11.             }  
  12.             public void display(int age, float marks)  
  13.             {  
  14.                 Console.WriteLine("Your age is : " + age);  
  15.                 Console.WriteLine("Your marks are :" + marks);  
  16.             }  
  17.         }  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Print obj = new Print();  
  21.             obj.display("George");  
  22.             obj.display(34, 76.50 f);  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Examples for Method Overriding in C#
  1. using System;  
  2. namespace methodoverriding  
  3. {  
  4.     class BaseClass  
  5.     {  
  6.         public virtual string YourCity()  
  7.         {  
  8.             return "New York";  
  9.         }  
  10.     }  
  11.     class DerivedClass: BaseClass  
  12.     {  
  13.         public override string YourCity()  
  14.         {  
  15.             return "London";  
  16.         }  
  17.     }  
  18.     class Program  
  19.     {  
  20.         static void Main(string[] args)  
  21.         {  
  22.             DerivedClass obj = new DerivedClass();  
  23.             string city = obj.YourCity();  
  24.             Console.WriteLine(city);  
  25.             Console.Read();  
  26.         }  
  27.     }  
  28. }  
Output

London

Association

Definition: When two objects are related with each other the relationship is called “Association” For example, the relationship:

Teacher X teaches Student Y

An association is usually drawn as a solid line connecting two classifiers or a single classifier to itself. Name of the association can be shown somewhere near the middle of the association line, but not too close to any end of the line.

Association wrote between Professor and Book with association ends author and textbook.

Look at the scenario “Manager uses a swipe card to enter XYZ premises”.

Scenario

The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object lifetime.

Aggregation

Look at the Scenario “Manager has workers who work under him”.

Aggregation

The Manager object will own Worker objects.

The child Worker objects cannot belong to any other object. For instance, a Worker object cannot belong to a SwipeCard object.
But, the Worker object can have its own lifetime which is completely disconnected from the Manager object.
Looking from a different perspective, it means that if the Manager object is deleted, the Worker object does not die.

Composition

Look at the scenarios:
  1. “Manager has the responsibility of ensuring that the project is successful”.

  2. “Manager's salary will be judged based on project success”.

    Composition

Conclusion

Manager and the project objects are dependent on each other.

The lifetimes of both the objects are the same. In other words, the project will not be successful if the manager is not good, and the manager will not get good increments if the project has issues.

Putting things together,

Putting things together

 Association Aggregation Composition
Owner No owner Single ownerSingle owner
Life time Have their own lifetime Have their own lifetimeOwner's life time
Child objectChild objects all are independentChild objects belong to a single parent Child objects belong to a single parent

Coupling and Cohesion

  • Coupling: “Coupling is the degree to which one class knows about another class”.
  • Cohesion: “It is used to indicate the degree to which a class has a single, well force purpose”.
Difference

Coupling is all about how classes interact with each other, on the other hand cohesion focuses on how single class is designed. Higher the cohesiveness of the class better is the OOP design.


Similar Articles