0% found this document useful (0 votes)
185 views43 pages

Object Oriented Programming Synopsis

1. Object Oriented Programming (OOP) is an approach that divides a program into objects that contain both data and functions. 2. The key concepts of OOP are encapsulation, inheritance, and polymorphism. 3. A class defines the data and functions that objects can use, while an object is an instance of a class. Classes support properties, methods, and constructors.

Uploaded by

ksurimca
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views43 pages

Object Oriented Programming Synopsis

1. Object Oriented Programming (OOP) is an approach that divides a program into objects that contain both data and functions. 2. The key concepts of OOP are encapsulation, inheritance, and polymorphism. 3. A class defines the data and functions that objects can use, while an object is an instance of a class. Classes support properties, methods, and constructors.

Uploaded by

ksurimca
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

OOPs

Object Oriented Programming


Synopsis
Introduction
Object Oriented Programming is an
approach that provides a way of
modularizing program by creating
partitioned memory area for both
data and functions that can be used
as templates for creating copies of
such modules on demand called
object.
Features of OOPs language

1. Emphasis on data rather than


procedure.
2. Program divided into objects (class).
3. Data is hidden and can not be
accessed by external functions.
4. Follows bottom-Up approach.
An object oriented programming
method has many advantages,
some of them are flexibility and
code reusability.
All the programming languages
supporting Object oriented
Programming will be supporting
these three main concepts:
About Object Oriented Languages
According to Bjarne Stroustrup, author of the C++
programming language, for a language to call
itself object-oriented, it must support three
concepts: objects, classes, and inheritance.

1. Fully Object Oriented Language:-.Net,Java.

2. Partially Object Oriented Language:-C++

3. Object based Language:- Vb6.0


Class and Objects
We can think of a class as simply a type (just
like char, int, or long) that has methods
associated with it .
Object is an instance of a type or class.

A class is a blueprint for a given set of


functionality, and an object created
based on a particular class has all the
functionality of the class built right in.
Instances of classes are called as Objects. They might
represent individual employees if Employee is defined as
a Class..
Three Pillars of OOPs
 Encapsulation
 Inheritance

 Polymorphism
Encapsulation
Encapsulation is process of keeping data
and methods together inside objects.
encapsulation is realized through the
classes. A Class can contain data and
methods.
Lke Employee class will provide properties
(Name,Age ....) as well as actions
(Calculatesalary, GoonLeave....) of the
employee entity.

Instances of classes are called as Objects. They might


represent individual employees.
Class
 class Account
 {
 int accno;
 string name;
 int balance;

 void Open_Account(int ac_no,string nm,int bal)


 {
 }

 void diposite (int acc_no,int amt)


 {
 }

 int withdraw(int acc_no,int amt)


 {
 }
 }
Function Overloading
Function overloading allows the existence of
multiple functions with the same name,
differing only in the parameters.
For example, if you were creating a method to print
a data, you might declare several functions, each
named Write.
One version would accept a string as a parameter;
another, an integer; and yet another, a
DateTime,object.
But Overloading allows to define many function
having different parameters but still the same
name.
 class Salary
 {
 int basic, comm, deduct, ta, ha =0;
 public void getnetsal(int b,int c,int d)
 {
 Console.WriteLine(b+c-d);
 }
 public void getnetsal(int b, int c, int d,int t,int h)
 {
 Console.WriteLine(b + c + t + h - d);
 }
 }
Properties
Properties are such members of classes,
structs or interfaces that provide a flexible
mechanism to read, write, or compute the
values of private fields through accessors.
Unlike fields, properties do not designate storage
locations. Instead, properties have accessors
that read, write, or compute their values.
[modifiers] type identifier {accessor-
declaration}
 Class transaction
 {
 string acc_id;
 int dipos,withd,bal;
 private string acc_no //read and write
 {
 set
 {
 acc_id=value;
 }

 get
 {
 return(acc_id);
 }
 }

 private int balance //read only


 {

 get
 {
 return(bal);
 }

 }

 private int withdraw //write only


 {
 set
 {
 withd=value;
 bal-=withd;
 }

 }
 }
Constructor and Destructor
Constructor is special function that is called automatically
whenever new instance is created.
It is used to initialize the values for class data member.
There can be any number of constructors in a program but
must be differ in terms of their parameters, Called as
Overloaded constructor.

Destructor is such special function that is invoked


automatically when an instance is destroyed.

Note :- The name of Constructor and Destructor must be the same name
as Class name where both never return any value.
Constructor can take one or more parameters but destructor can not.
Destructor starts with (~) titles character.
 Account() //Default
 {
 }

 Account(int ac_no,string nm,) //Overloaded


 {
 }

 Account(int ac_no,string nm,int bal) //Overloaded


 {
 }

 ~Account() //Destructor
 {
 }
Objects
Object is an instance of a type or
class.

Instances of classes are called as Objects.


For example there might be three instances of the
Employee - Ram , Raj and Tom.

They might represent individual employees.


 Employees Ram=new Employee();
 Or Employee Raj;
 Raj=new Employee();
 Or Customer abc=new
customer(101,”hyd”); //Calling
Overloaded constructor
Inheritance
Inheritance relates to the programmer's ability to
specify that one class has a kind-of relationship
with another class. Through inheritance, you can
create (or derive) a new class that's based on an
existing class.
In a few words, Inheritance is the process of
creation new classes from already existing
classes.
inheritance feature allows us to reuse some parts of
code .
The .NET framework has many base
classes. Everything is derived from
System.Object .
Class class1 {………………………….}
Class class2 : class1 {………………….}
 class class1
 {

 public int i;
 }

 class class2 : class1


 {
 private void f1()
 {
 i = 10;
 }
 }
Types Of Inheritance
1. Singe Level or Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single Level Multi level Multiple

Base Class Base Class Class1 Class2

Child Class

Class12

Child Class Grand Child


Hierarchical Hybrid

. Base Class Base Class

Hierarchical

Child1 Child2
Child1 Child2
Multiple

Child12
Child11

Multiple Inheritance is not supported by in .Net.


Multiple inheritance
Multiple inheritance is the possibility that a child
class can have multiple parents. Human beings
have always two parents, so a child will have
characteristics from both parents.
In OOP, multiple inheritance might become difficult
to handle because it allows ambiguity for the
compiler. There are programming languages
such as C++ that allow multiple inheritance;
however, other programming languages such as
Java and the .NET Framework languages do not
allow multiple inheritance. Multiple inheritance
can be emulated in .NET using Multiple Interface
Inheritance .
Access Modifiers
private Access limited to the containing type.

protected Access limited to the containing class or types derived


from the containing class.

internal Access limited to this program.

Protected internal Access limited to this program or types derived


from the containing class outside the program.

public Access not limited.


***By default All members(data & function) will be Private.
Sealed class
Some object model designs need to allow the
creation of new instances but not inheritance, if
this is the case, the class should be declared as
sealed.

A sealed class is a class that does not allow


inheritance.

To create a sealed class in C# :-


sealed class Shape

To create a sealed class in VB.NET, :


NonInheritable Class Shape
Abstraction
Abstraction is "the process of identifying common
patterns that have systematic variations; an
abstraction represents the common pattern and
provides a means for specifying which variation
to use" .
Great example is a bank account. People own
savings accounts, checking accounts, credit
accounts, investment accounts, but not generic
bank accounts.
In this case, a bank account can be an abstract
class and all the other specialized bank accounts
inherit from bank account.
An abstract class is a parent class that
allows inheritance but can never be
instantiated.
Abstract classes contain one or more
abstract methods that do not have
implementation. Abstract classes
allow specialization of inherited
classes.
To create an abstract class in C# :-

abstract class Shape

To create an abstract class in VB.NET,


MustInherit Class Shape
 abstract class Vehicle
 {
 public float weight;
 public byte tyre;

 public abstract string start(); //abstract function


 public void stop() //unabstract function


 {
 Console.WriteLine("Vehicle Stoped");
 }

 }

 Note :- An abstract class can have zero or more abstract as well as


unabstract function.
 But an abstract function can only be declared inside Abstarct class.
 But abstract function does not have body part but only declration.
 The abstarct function must be public.
 class car :Vehicle
 {
 public override string start()
 {
 return("started");
 }
Note :- If a child class inherits from a abstract class , the
child class has to define the body of all abstract function
existing into base class using override keyword.
Virtual keyword
A virtual property or method has an implementation in the
base class, and can be overriden in the derived classes.
The virtual keyword allows polymorphism too.

To create a virtual member in C#:-

public virtual void Draw() {-------} --Base class Member


Public override void Draw(){-----} Child class

To create a virtual member in VB.NET :-


Public Overridable Function Draw()
 class clas3
 {
 public virtual void vf()
 {
 }
 Public void f1()
 {
 }
 }

 class cls4 : clas3


 {
 public override void vf()
 {
 }
 Public void new f1()
 {
 }
 }
Override keyword
Overriding is the action of modifying or
replacing the implementation of the
parent class with a new one.
Parent classes with virtual or abstract
members (in Abstract class) allow
derived classes to override them.
 To override a member in C# :-

public override void CalculateArea()


 To override a member in VB.NET :-
Public Overrides Function CalculateArea()
Interface
To rectify the drawback of multiple
inheritance, the creators of C# have
introduced a new concept called
interfaces.
. Interface's members can be Events,
Methods, Properties and Indexers. But
the interface contains only declaration
for its members.
The interface can't contain constants, data
fields, constructors, destructors and
static members .
 interface Iface
 {
 void send(string msg);
 string get();
 }

Note :-Interface Members are by default Public.


A field can not be defined within an Interface.
 class Msg : Iface
 {
 string message;
 public void send(string msg)
 {

 message=msg;
 }

 public string get()


 {
 return(message);
 }
 }
Note :- A class can Implement any number of Interfaces.
class Msg : Iface,Iface2 { ------- }
The class has to implement all of the members declared into Iface and Iface2.
Avoiding Name Ambiguity

interface Interdemo
{ void Show();}
interface Interdemo1
{ void Show();}

class Interclash:Interdemo,Interdemo1
{ void Interdemo.Show() {
Console.WriteLine("Show() method Implemented"); }
void Interdemo1.Show() {
Console.WriteLine("Display() method Implemented"); }

To call the members :-


Interclash inter = new Interclash();
inter.Interdemo.Show();
inter.Interdemo1.Show(); }
Why Use Interfaces?

 To allow a class to inherit multiple


behaviors from multiple interfaces.
 To avoid name ambiguity between the
methods of the different classes as
was in the use of multiple inheritance
in C++.
 To combine two or more interfaces
such that a class need only implement
the combined result.
Note
 constructor also inherits base class constructor.
 To override already defined method (into base class ) into child class a
keyword new is used.
 A virtual function can have body in base class but a abstract function
can never have body in base class.
 A virtual function can reside in abstract class as well as unabstract
class also.
 A abstract function can only be appear in abstract class.
 A abstract class can have abstract as well as unabstract function also.
 A virtual and abstract both type of function can be overidden in child
class using override keyword.
 An Object can not be created for Abstract classes or Interfaces.
 Interface can not contain any field (variable).
 An abstract can contain field and unabstract functions also but
Interface can contain only abstract functions.
 An Interface can be defined within a class also.
Operator Overloading
Operator is used to defined, what function need to perform with the
Operands.

Like res= num1 + 100;

The Operator Overloading is a way to redefine the functionality


Of an existing Operator.

This gives the operator more than one meaning, or "overloads" it. The
compiler distinguishes between the different meanings of an
operator by examining the types of its operands.

You can redefine the function of most built-in operators globally or on


a class-by-class basis. Overloaded operators are implemented as
functions.
Example
 class op_cls
 {
 int num1;

 public op_cls()
 {
 num1=0;
 }

 public op_cls(int arg)


 {
 num1=arg;
 }

 public static op_cls operator +(op_cls obj1,op_cls obj2)


 {
 op_cls obj3=new op_cls();

 obj3.num1=obj1.num1+obj2.num1;

 return(obj3);
 }

 public void show()


 {
 Console.WriteLine(num1.ToString());
 }

 }
Using Overloaded Operator
 op_cls o1=new op_cls(100);
 op_cls o2=new 0p_cls(200);

 op_cls o3=new op_cls();

 o3=o1+o2;

 o3.show(); //300
Overloadable Operators

 +, -, !, ~, ++, --,  These unary


true, false operators can be
overloaded.
 +, -, , /, %, &, |, ^,  These binary
<<, >> operators can be
overloaded.
 These operators
 =, ., ?:, ->, new, is,
cannot be
sizeof, typeof
overloaded.

You might also like