0% found this document useful (0 votes)
12 views35 pages

VP - Lecture 8 - OOP Overview

visual programming

Uploaded by

Aneeza Yasmeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views35 pages

VP - Lecture 8 - OOP Overview

visual programming

Uploaded by

Aneeza Yasmeen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Visual Programming

Course Overview & IDE Setup


Rida Bajwa
Recap …

• Previous Discussion:
⮚ Debugging and Error Handing
Today’s Discussion
⮚ Object Oriented Programming

⮚ Object, Fields and properties

⮚ Accessibility

⮚ Methods

⮚ Object lifecycle, Constructors & Destructors

⮚ Static Fields/Members

⮚ General OOP Concepts


OBJECT-ORIENTED
PROGRAMMING
WHAT IS OBJECT-ORIENTED
PROGRAMMING?
• Seeks to address many of the problems with traditional
programming techniques.
• The type of programming so far is known as procedural
programming
• results in so-called monolithic applications,
• meaning all functionality is contained in a few modules of code
(often just one)

• In traditional application, the flow of execution is often simple


and linear
• Applications are loaded into memory, begin executing at
point A, end at point B, and are then unloaded from
memory.
WHAT IS OBJECT-ORIENTED
PROGRAMMING?
• With OOP techniques, can use many more modules of code,
with each offering specific functionality.
• modules can be isolated or even completely independent of
the others.
• This modular method provides more versatility and code
reuse.

• For Example an example of engine replacement in a car


WHAT IS OBJECT-ORIENTED
PROGRAMMING?
• With OOP, things are rarely so linear, way of getting there is
often very different.
• firmly rooted in the structure and meaning of data,
• and the interaction between that data and other data.
• means putting more effort into the design stages of a
project, has the benefit of extensibility.
• OOP often simplifies things by providing an
agreement about
• the approach to data representation,
• the structure and usage of abstract entities

• For example, an agreement made with a device and


a printer,
What Is an Object?
What Is an Object?
• Everything in C# and the .NET Framework is an object
• An object is a building block of an OOP application.
• Encapsulates part of the application, which can be a
process, a chunk of data, or a more abstract entity
• Can represent a process by containing only functions
• Just like the variables created from types
• The type of an object is known by a special name “class”
• A class is the template, or perhaps the plans used to build
the object/instance.
Properties and Fields
• Provide access to the data contained in an object
• Make up the state of that object.
• Must provide it with a state for it to be meaningful

• Imagine an object class that represents a cup of coffee,


called CupOfCoffee
• set the type of coffee used,
• whether the coffee contains milk and/or sugar,
• whether the coffee is instant,
• and so on
Properties and Fields
• Properties differ from fields in that they don’t provide direct
access to data
• For fields can store values limited only by the limits of the
type used to store this information
• for example, for an int could use any value between
−2147483648 and 2147483647

• If a property is used could limit this value to, say, a number


between 0 and 2.
Properties and Fields
• Better to provide properties rather than fields for state
access
• have more control over various behaviors

• Read/write access to properties can also be clearly defined


by an object.
• properties can be read-only or write-only properties
• read-only property of the CupOfCoffee class called
Description
Accessibility
• Specify a sort of access permission for both fields and
properties

• determines which code can access these members


• available to all code (public),
• only to code within the class (private)
• Or its related one (protected)

• common practice is to make fields private and provide


access to them via public properties
Accessibility
• Code within the class has direct access to data
stored in the field
• Public property shields external users from this data
• Prevents them from placing invalid content

• Public members are said to be exposed by the class


Methods
• Method is the term used to refer to functions exposed by
objects
• used in the same way as any other function
• provide access to the object’s functionality.

• Like fields and properties, can be public or private,


restricting access to external code as necessary.
• Have access to private members
Methods
• Every command used has been a property or a
method, such as
• < String >.Length ,
• < String >.ToUpper() ,
• and so on.
The Life Cycle of an
Object
The Life Cycle of an Object
• Apart from the normal state of “being in use,” the life cycle
includes two important stages:

• Construction —When an object is first instantiated it needs


to be initialized.
• carried out by a constructor function
• often referred to simply as a constructor for convenience.

• Destruction —When an object is destroyed, there are often


some clean-up tasks to perform, such as freeing memory.
• The job of a destructor function, also known as a destructor
Constructors
• Basic initialization of an object is automatic
• to perform additional tasks during an object’s initialization
• All class definitions contain at least one constructor
• Can include a default constructor,
• A parameter-less method with the same name as the
class itself
• might also include several constructor methods with
parameters, known as nondefault constructors.
• perhaps providing initial values for data stored in the
object
Constructors
• constructors are called using the new keyword.
• CupOfCoffee myCup = new CupOfCoffee();
• CupOfCoffee myCup = new CupOfCoffee("Blue Mountain");

• can be public or private


• a class can’t instantiate an object using a private
constructor; it must use a public constructor.

• Some classes have no public constructors,


• it is impossible for external code to instantiate them (they
are said to be noncreatable)
Destructors
• Destructors are used by the .NET Framework to clean up
after objects.

• Provide specific instructions if anything important needs to


be done before the object instance is deleted.
• For example, when a variable goes out of scope
• to perform its garbage collection clean-up

• Don’t have to provide code, default operation does the work


Static and Instance Class Members
• Also known as shared members
• shared between instances of a class
• Can be thought of as global
• To access data that is independent of any object
• Execute commands related to the class type but not specific
to object instances
• don’t even need to instantiate an object

• For example, the Console.WriteLine() and Convert.ToString()


methods you have been using are static.
Static and Instance Class Members
• Many useful situations
• For example, using a static property to keep track of how
many instances of a class have been created.
Static Constructors
• To initialize static members beforehand.
• Can supply a static member with an initial value
• Can use a static constructor to perform this initialization
• Class can have a single static constructor,
• Must have no access modifiers and cannot have any
parameters.

• Can never be called directly


• Called only once
Static Constructors
• Executed when one of the following occurs:
• An instance of the class containing the static constructor
is created.
• A static member of the class containing the static
constructor is accessed.

• static constructor is called first, before the class is


instantiated or static members accessed.
• All non-static constructors are also known as instance
constructors
Static Classes
• Can never be instantiated
• Contain only static members and cannot be used to
instantiate objects
• such as Console
• Can’t have instance constructors

• Rather than making the constructors of the class private use


a static class
• Static classes can have a static constructor
OOP Concepts
OOP Concepts
• Encapsulation is the combination of the data and actions
that are related to an object.
• For example, a BankAccount type might have data,
such as Balance and AccountName, as well as
actions, such as Deposit and Withdraw.

• Composition is about what an object is made of.


• For example, a Car is composed of different parts, such
as four Wheel objects, several Seat objects, and an
Engine
OOP Concepts
• Aggregation is about what can be combined with an object.
• For example, a Person is not part of a Car object, but
they could sit in the driver’s Seat and then become the
car’s Driver—two separate objects that are aggregated
together to form a new component

• Inheritance is about reusing code by having a subclass


derive from a base or superclass.
• All functionality in the base class is inherited by, and
becomes available in, the derived class.
OOP Concepts
• Abstraction is about capturing the core idea of an object
and ignoring the details or specifics.
• C# has the abstract keyword.
• If a class is not explicitly abstract, then it can be
described as being concrete
• Base or superclasses are often abstract
• for example, the superclass Stream is abstract, and its
subclasses, like FileStream and MemoryStream, are concrete.

• Only concrete classes can be used to create objects;


• abstract classes can only be used as the base classes
because they are missing some implementation.
OOP Concepts
• Polymorphism is about allowing a derived class to override
an inherited action to provide custom behavior.
Wrap up
Summary
• We discussed:
⮚ Object Oriented Programming

⮚ Object, Fields and properties

⮚ Accessibility

⮚ Methods

⮚ Object lifecycle, Constructors & Destructors

⮚ Static Fields/Members

⮚ General OOP Concepts

You might also like