Module 8 - Essentials of Object Oriented Programming
Module 8 - Essentials of Object Oriented Programming
Module 8 - Essentials of Object Oriented Programming
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.
Object
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;
}
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.
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
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.
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 :-
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.
STATIC BINDING
DYNAMIC BINDING
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..
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