You are on page 1of 26

Basics of OOPS

Need for OOP


Languages like c use the procedural programming
approach for application development.

In this approach, we divide the application into several tasks


and write code for all tasks.

Therefore, the focus in this approach is on the tasks and not on


the objects

The traditional programming approach was not able to handle


the increasing demand of software applications.
The drawbacks of procedural Programming approach
are -

Unmanageable Programs
Code Reusability not Optimum

Problems in Modification of Data

Difficulty in Implementation
Object-Oriented Programming Language

object-oriented programming is about creating


objects that contain both data and methods
For a programming language to be a true OOP
language, the language must meet the following
criteria:
• Abstraction
• Encapsulation
• Polymorphism
• Inheritance
Classes and Objects
• Classes and objects are the two main aspects of
object-oriented programming.
• Simply a class is a blueprint, and an object is a
building made from that blueprint
• So, the act of creating an object is called
instantiation
• A class is a mould or a template of an object.
• A class is what defines all the data members and the method
that an object should have.
• The class defines the characteristics that the object will
possess;
it could also be referred to as a blueprint of the
object.

• Each object is said to be an instance of the class.


Classes are declared by using the keyword class
followed by the class name and a set of class
members surrounded by curly braces { }.

using System;

public class House


{
...
}

public class Program


{
public static void Main()
{
int bedrooms = 3; // assuming bedrooms defined in
House
}
}
Basic syntax for class declaration

Class comprises a class header and class body.

public class class_name


{
class body
}

Note: No semicolon (;) to terminate class block.

But statements must be terminated with a semicolon ;


Class body comprises class members – constructor, data
fields and methods.
• Properties – A characteristic required of an object when
represented in a class is called a property or a f
• Method – A method is similar to a function, routine or a
procedure; here it is associated to a class.
• Constructor– A constructor is a special method that is
used to initialize objects. The advantage of a
constructor, is that it is called when an object of a
class is created. It can be used to set initial values for
fields.
C# - Basics: Classes

• Declaration

public Class MyClass {...}

• Constructors

public MyClass(parameters) {...}

• new operator – create an instance of the class

MyClass objMyClass = new MyClass();


An object represents an entity in the real world.
An object is simply something that is relevant to a particula
application.
An object is a concept or a thing, with defined boundaries,
that is relevant to the problem being dealt with.
Access Modifiers
Access modifier is used to set the access level/visibility for
classes, fields, methods and properties
C# has the following access modifiers

Modifier Description
public The code is accessible for all classes
. private The code is only accessible within the same class

protected The code is accessible within the same class, or in a class that
is inherited from that class.

internal The code is only accessible within its own assembly, but not
from another assembly.
o We can incorporate the functionality of existing classes into
any other class just by inheriting the new class from the
existing class.
o The existing class is called the BASE class and the newly
created class is called the DERIVED class.
Inheritance
Enables you to
– Create a general class and then define specialized classes
that have access to the members of the general class

A derived (or child) class inherits from a base (or parent) class

The derived class inherits all the attributes and behaviour of the
base class.

The derived class may implement also its own attributes and
behavior.

The derived class may override inherited attributes and behaviour


Inheritance hierarchy for Shapes.
public class ChildClass : ParentClass
using System;
{
public ChildClass()
public class ParentClass
{
{
Console.WriteLine("Child
public ParentClass()
Constructor.");
{
}
Console.WriteLine("Parent
Constructor.");
public static void Main()
}
{
ChildClass child = new ChildClass();
public void print()
{
child.print();
Console.WriteLine("I'm a Parent
}
Class.");
}
}
}

Child class object invokes print() inherited from


parent class.
Extending Classes

• Use a single colon


– Between the derived class name and its base class name
• Inheritance works only in one direction
– A child inherits from a parent

public class ChildClass : ParentClass


{
...
}
Data abstraction enhances security as use of data is restricted
to certain functions only.
Abstraction is more used where you want only a certain
number of functions/methods are accessing data.
Abstraction
Example
Grouping of data and methods into a single
entity is known as Data Encapsulation.

Encapsulation hides the internal implementation


of an abstraction within the particular object
Object – Oriented languages try to make existing code easily
modifiable without changing the code much.
Polymorphism allows functions to take more than one form.

Polymorphism enables the same method to behave differentl


on different classes.
For example, think of a base class called Animal that has a method
called animalSound().Derived classes of Animals could be Pigs,
Cats,Dogs, Birds -And they also have their own implementation of
an animal sound (the pig oinks, and the cat meows, etc.):
using System;
namespace MyApplication
{
class Animal // Base class (parent)
{
public virtual void animalSound()
{
Console.WriteLine("The animal makes a
sound");
}
}
class Pig : Animal // Derived class (child)
{
public override void animalSound()
{
Console.WriteLine("The pig says: wee wee");
}
}
class Dog : Animal // Derived class (child)
{
public override void animalSound()
{
Console.WriteLine("The dog says: bow wow");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
}
Benefits of Object Oriented Programming approach
are -

 Help us to thinking in terms of Real World Objects

 OOP offers better data security

 OOP offers better code reusability

 OOP offers more flexibility,faster execution

 OOP offers better manageable programs


THANK YOU

You might also like