Module 8 - Essentials of Object Oriented Programming

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 51

Module 8:

Essentials of Object
Oriented Programming

By

SRIRAM . B
Overview
 Object Oriented Approach & Programming
 Components of Object Oriented Programming
 Properties, Fields & Methods
 Life Cycle of an object
 Inheritance
 Polymorphism
 Diff. Between Overloading & Overriding
 Function Overloading
 Overriding
Object Oriented Approach
 The concept behind the object-oriented approach is to combine
data and its functions into a single unit.

 The data pertaining to an object is also known as an attribute.

 The functions of an object are also known as methods.

 An object’s functions provide the only way to access its data.


Thus, the data available in an object is protected from
accidental alteration.
Object Oriented Programming

 Problem solving technique to develop software


systems.

 Technique to think real world in terms of objects.

 Object maps the software model to real word


concept. These objects have responsibilities and
provide services to application or other services.

 Eg. OOP'S – C++, C#, Java, etc..


Components of Object Oriented
Programming
 Class

 A group of objects which have similar data and


methods is called a class.

 Object

 An object is an instance of a class.

For example if student is a class, then Michael


and John are instances. The data components that
are common among these objects are Name, Batch
code etc., and the functions that are common are
Attend_class(), Attend_test() etc.
Characteristics of an Object
Oriented Language
 Inheritance

 Inheritance is the process of creating a new class,


called the derived class, from an existing class,
called the base class.
 Reusability

 The concept of inheritance provides for reusability


of existing classes and their functions.
 Polymorphism

 The concept of using operators or functions in


different ways is called polymorphism.
Characteristics of an Object
Oriented Language..
 Encapsulation

 The concept of information hiding or hiding of


non-essential details of a program.
 Abstraction

 It is the process of picking out or abstracting


essential features of an entity that describes it
completely.
Example1 – Creating Classes &
Object
Consider the code given below which creates a class,
Student, and an object, John.

using System;
class Student
{
string name;
public void disp(){
name=“John”;
Console.WriteLine("Name of the student is {0}", name);
}
Example 1 – Creating Classes &
Object
public static void Main()
{ Student john = new Student();
john.disp();
}
}
Example 2 – Creating Classes &
Object
Member functions can be used to manipulate data
members of a class as follows:
using System;
class Student
{
string name;

public void Assign_details()

//Assigns John to the member data name


{
name="John";
}
Example 2 – Creating Classes &
Object

public void Display_details()


{
Console.WriteLine("Name of the student is
{0}",name);

//Displays the contents of name

}
Example 2 – Creating Classes &
Object
public static void Main()
{
Student john = new Student();
john.Assign_details();
//Invokes the function Assign_details()
john.Display_details();
//Invokes the function Display_details()
}
}
Properties, Fields & Methods
 A property is a member that holds data of an object or class.
Properties are natural extensions of fields – both are named
members with associated types, and the syntax for
accessing fields and properties is same.

 However unlike fields, properties do not denote storage


locations. Instead properties have accessors that specify the
statements to execute in order to read or write their values.

 Properties provide a mechanism for associating actions


with the reading and writing of an object's attributes and
furthermore permit such attributes to be computed.
Properties, Fields & Methods..
 The accessor of a property contains the executable
statements associated with getting (reading or computing ) or
setting (writing) the property. There are two types of
accessors.

* get ( readonly property)

* set ( writeonly property)

 Get accessor corresponds to a parameterless method with a


return value of the property type . And the body of a get
accessor must conform to the rules for value returning
methods.

 A set accessor corresponds to a method with a single value


parameter of the property type and a void return type.
Properties, Fields & Methods..
 Methods : -

A method is simply the action that a message


carries out. It is the code, which gets executed when
the message is sent to particular object.

It is function exposed by the objects. These may be


called in the same way as any other function, and
may use return value and parameters in the same
way.

They are used to give access to the functionality of


objects.
Life Cycle of an Object
 Every object has a life cycle, which lasts from using a
class definition to its destruction.

 There are two important stages in object's life


cycle :-

 Construction :- When the object is first instantiated.


The initialization is called as construction and carried
out by a Constructor function.

 Destruction :- When an object is destroyed there will


often be some clean up tasks to perform such as freeing
up memory by Destructor function.
Constructors
 A constructor is a member that is used to perform actions
required to initialise an instance of a class.
 A constructor has to be declared using the private / public access
specifier.
 A class can have multiple constructors.
 The constructor method should have the same name as the
class.

 If the constructor is private it can be instantiated only in the class


where it has been defined.

 A constructor can also be static, which is executed only once when


the class is loaded. Static constructor is associated with the class
which is executed once but the instance constructor is called
whenever a new object is created.
Example 1 - Constructors
using System;
class Mycons
{
string name;
public void disp()
{
Console.WriteLine("Value of name is
{0}",name);
}
public Mycons()
// Constructor without arguments
Example 1 - Constructors
{ name="Michael";
}
//Constructor with a string parameter
public Mycons(string str)
{
name=str;
}
Example 1- Constructors

consobj.disp();
Mycons consobj2=new Mycons(“Mary”);
//Invokes the second constructor
consonj2.disp();
}
}
Example 2 - Constructors
using System;
class Myclass
{ string name;
public void disp()
{
Console.WriteLine("Value of name is
{0}",name);
}
public static void Main()
{
Myclass cc=new Myclass();
cc.disp();
} }
Destructors

 A destructor is a member that is used to destroy an


object of a class.

 A destructor is a method with the same name as the


class preceded by the symbol, ~.

 A destructor cannot take parameters.

 A class can have only one destructor.


Automatic Memory Management
 Garbage collection:

 Is a process that automatically frees the memory of objects


that are no more in use.

 Enables a programmer to automatically free allocated


memory.

 Identifies the objects that are no more referenced in the


program and releases the memory allocated to them.
Example - Destructor
using System;

namespace SriConsole.OOPS
{
class Destructor
{
static void Main(string[] args)
{
aa a = new aa();
Console.ReadLine();
}

}
Example - Destructor
public class aa
{
public aa()
{
Console.WriteLine("Constructor ");
}
~aa()
{
Console.WriteLine("Destructor");
}
}

}
Inheritance
 The main aim of inheritance is to improve code reusability.

 This is done by creating classes that derive properties


from other classes and are known as derived classes.

 The class from which the derived class derives properties


is known as the base class.

 Classes support single inheritance.

 The Object class is the ultimate base class for all classes.
Different Forms of Inheritance
 Single inheritance (Only one superclass)
 Multiple Inheritance ( several super classes)
 Hierarchical inheritance ( one superclass, many
subclasses)
 Multilevel inheritance (subclass derived from
another subclass)
Some of the important aspects of
Inheritance :-

 Inheritance is transitive. If C is derived from B, and B is


derived from A, then C inherits the members declared in B
as well as the members declared in A.

 A derived class extends its direct base class. A derived


class can add new members to those it inherits, but it
cannot remove the definition of an inherited member.

 A derived class can hide inherited members by declaring


new members with the same name or signature. Note
however that hiding an inherited member does not remove
the member - it merely makes the member inaccessible in
the derived class.
Example 1 - Inheritance
using System;
class Base
{ public void func()
{
Console.WriteLine("In base");
}
}
Example 1 - Inheritance..
class Derived:Base
{ public void func1()
{
Console.WriteLine("In derived");
}
}
Example 1 - Inheritance..

class Unrelated
{
public static void Main()
{
Derived derivedobj=new Derived();
derivedobj.func();
// func() method inherited by Derived class
from Base class
derivedobj.func1();
//func1() method introduced in Derived class
}
}
Polymorphism
 Polymorphism means the ability to take more then one form.

 For example , an operation may exhibit different behavior in


different instances. The behavior depends upon the types of data
used in the operation.

 The concept of polymorphism is often expressed by the phrase


"one interface multiple methods". This means that it is possible to
design a generic interface to a group of related activities.

 The specialized forms of polymorphism :-


 static binding
 dynamic binding
 And the form of method overloading and overriding in c#.
Binding

 It refers to the linking of a method call to the code to


be executed in response to the call.

 STATIC BINDING

 The object checks for a method call in the class.


The method when found is executed. when this
happens at the compile time, it is called static
polymorphism.
Binding..

 DYNAMIC BINDING

 Sometimes it is not possible for the compiler to


decide which method should be called.

 It means that the code associated with a given


procedure call is not known until the time of the
call at runtime. this happens when the same
method name is used to perform different
functionality by overriding it in the derived or sub
class.
Difference between Overloading &
Overriding
 Overloading takes place within the same class i.e.,
Overloaded forms of same function or within the
same class.

 Where as in overriding when the class is subclassed


and the function is made to perform different
functionality in the subclass it is referred to as
overriding.
Function overloading
 Function overloading is the concept of using the same
name for two or more functions.
 Each redefinition of the function should have a unique
signature.
 Function overloading is used to implement
Polymorphism.
 A function signature consists of the:
 Method name
 Type of parameters
 Sequence of parameters
 Number of parameters
Function overloading..

 The resolution of an overloaded function depends on


the list of arguments and a set of candidate member
functions and the selection of the function to invoke is
as follows:
 By the invocation of a named method
 By the invocation of a constructor

 The selection of the best function to be invoked is


done from a set of applicable candidate functions.
 A member is called a candidate function based on the
argument list.
 A function is a best member function, when its
arguments match the function called.
Function overloading..
 The new operator is used to create new instances of
a class.
 The new operator can be used as follows:
 In object creation
 In array creation
Example 1 - Function overloading

using System;
class Add_numbers
{
int x,y;
public void add() //First add method
{ x=y=0;
int sum=x+y;
Console.WriteLine(“Result is {0}”,sum);
}
Example 1 - Function overloading..

public void add(int a, int b)


//Second add method
{
int sum=a+b;
Console.WriteLine(“Result is {0}",sum);
}
Example 1 - Function overloading..
public static void Main()
{
Add_numbers obj=new Add_numbers();
//Invokes the first add method
obj.add();
//Invokes the second add method
obj.add(10,20);
}
}
Overriding
 Overriding is used to improve or modify the
functionality of existing methods.

 A base class member can be overridden by defining a


derived class member with the same name as that of
the base class member.

 The keyword override is used to define a method in


the derived class that overrides the method of the
base class.
Example 1- Overriding Base Class
Members
using System;
class Base
{ public virtual void disp()
//Base class method declared as virtual
{
Console.WriteLine("Base");
} }
class Derived : Base
{ public override void disp() //override
keyword
{
Example 1- Overriding Base Class
Members..
Console.WriteLine("Derived");
}
public static void Main()
{
Derived d=new Derived();
//Derived class object
d.disp();
//disp()method of the derived class
}
}
Namespace
 A namespace is a collection of classes.
 Namespaces provide re-usability of existing classes.

 Predefined namespaces include classes to perform basic input-


output operations.
 For example,
 Console is a class that belongs to the predefined namespace,
System.
 Namespaces are used to create a group of logically related classes.
Differentiate between System.Console.WriteLine(); &
using System;
Console.WriteLine();

 A .cs file can be converted to a .dll using the command,”


csc /t:library <filename>
Example 1 – Creating Namespace

namespace Namespacename //Declare the


namespace
{
class classname
{
//Functions within the class can be given
here
}
}
Example 1 – Namespace..

using System;
namespace Mynamespace //Declare the namespace
{
public class HelloWorld //Define the class
{
public static int SayHi() //Member
function
{
Console.WriteLine("Hello, World!");
return 0;
}
}
}
Example 1 – Namespace..
using System;
using Mynamespace; //Using the user-defined
namespace
class CallingMynamespace
{
public static void Main(string[] args)
{
//call the SayHi() method of
Mynamespace.HelloWorld class

HelloWorld.SayHi();
}
}
Session Ends
Exercise
Relax

You might also like