C Oops
C Oops
C Oops
C# .Net
(OOPS Material)
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Index:
1. Introduction
2. Programming elements (variables & methods)
3. Classes and Objects
4. Static methods
5. Static variables
6. Instance methods
7. Object creation
8. Constructor
9. Instance variables
10. This and parameterized constructor
11. Access specifiers
12. Encapsulation
13. Constructor chaining using this()
14. Inheritance
15. Virtual and protected keywords
16. Method overriding
17. Base keyword
18. Base()
19. Polymorphism
20. sealed modifier or sealed classes
21. abstract classes
22. interfaces
23. Multiple inheritance through interface
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Application:
Programming Languages and Technologies are used to develop applications.
Application is a collection of Programs.
We need to design and understand a single program before developing an application.
1. Identity:
o Identity of a program is unique.
o Programs, Classes, Variables and Methods having identities
o Identities are used to access these members.
2. Variable:
o Variable is an identity given to memory location.
or
o Named Memory Location
o Variables are used to store information of program(class/object)
Syntax Examples
3. Method:
Method is a block of instructions with an identity
Method performs operations on data(variables)
Method takes input data, perform operations on data and returns results.
Syntax Example
returntype identity(arguments) int add(int a, int b)
{ {
body; int c = a+b;
} return c;
}
Introduction to Object oriented programming:
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Note: Class is a Model from which we can define multiple objects of same type
Encapsulation:
The concept of protecting the data with in the class itself.
Implementation rules: (POJO rules)
o Class is Public (to make visible to other classes).
o Variables are Private (other objects cannot access the data directly).
o Methods are public (to send and receive the data).
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Inheritance: Defining a new class by re-using the members of other class. We can implement
inheritance using “extends” keyword.
Terminology:
o Parent/Super class: The class from which members are re-used.
o Child/Sub class: The class which is using the members
Abstraction:
Abstraction is a concept of hiding implementations and shows functionality.
Abstraction describes "What an object can do instead how it does it?".
Polymorphism:
Polymorphism is the concept where object behaves differently in different situations.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Variables in C#
Variables:
Variable stores information of class(object).
Variables classified into 4 types to store different types of data.
1. Static Variables: Store common information of all Objects. Access static variables using
class-name.
2. Instance Variables: Store specific information of Object. Access instance variables using
object-reference.
3. Method Parameters: Takes input in a Method. Access Method parameters directly and
only inside the method.
4. Local Variables: Store processed information inside the Method. Access local variables
directly and only inside the method.
Methods
Method:
A block of instructions having identity.
Methods takes input(parameters), process input and return output.
Methods are used to perform operations on data
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Syntax Example
returntype identity(arguments){ int add(int a, int b){
statements; int c=a+b;
} return c;
}
Classification of Methods: Based on taking input and returning output, methods are classified
into 4 types.
Method Definition: Method definition consists logic to perform the task. It is a block.
Method Call: Method Call is used to invoke the method logic. It is a single statement.
Static Method: Defining a method using static keyword. We can access static methods using
class-name.
static void display()
{
logic;
}
Instance Method: Defining a method without static keyword. We can access instance methods
using object-reference.
void display()
{
logic;
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Static Variables
Static Variable:
Defining a variable inside the class and outside to methods.
Static variable must define with static keyword.
Static Variable access using Class-Name.
class Bank
{
static String bankName = "AXIS";
}
Note: We always process the data (perform operations on variables) using methods.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
It is recommended to define get() and set() method to each variable in the class:
public class Program
{
public static void Main()
{
First.setA(10);
First.setB(20);
First.setC(30);
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Instance Members in C#
Instance Members:
Instance members also called non-static members.
Instance members related to Object.
We invoke instance members using Object-address
Constructor:
Defining a method with Class-Name.
Constructor Not allowed return-type.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
class Employee{
public Employee(){
Console.WriteLine("Object created");
}
}
Instance Method:
Defining a method without static keyword.
We must invoke the method using object-reference.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
void fun(){
Console.WriteLine("fun");
}
}
With arguments and No return values method:
public class Program
{
public static void Main(){
Program obj = new Program();
obj.isEven(12);
}
void isEven(int n){
if(n%2==0)
Console.WriteLine("even");
else
Console.WriteLine("odd");
}
}
With arguments and with return values method:
public class Program{
public static void Main(){
Program obj = new Program();
int sum = obj.add(10, 20);
Console.WriteLine("Sum is = " + sum);
}
int add(int a, int b){
int c = a+b;
return c;
}
}
No arguments and with return values:
public class Program
{
public static void Main(){
Program obj = new Program();
double PI = obj.getPI();
Console.WriteLine("PI value is = " + PI);
}
double getPI(){
double PI = 3.142;
return PI;
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Instance Variables:
Defining a variable inside the class and outside to methods.
Instance variables get memory inside every object and initializes with default values.
this:
It is a keyword and pre-defined instance variable in C#.
It is also called Default Object Reference Variable.
“this-variable” holds object address.
o this = object_address;
It is used to access object inside the instance methods and constructor.
Parameterized constructor:
Constructor with parameters is called Parametrized constructor.
We invoke the constructor in every object creation process.
Parameterized constructor is used to set initial values to instance variables in Object
creation process.
Note: We invoke the constructor with parameter values in object creation as follows
public class Program
{
int a;
Program(int a){
this.a = a;
}
public static void Main(){
Program p = new Program(10); //pass parameter while invoking constructor
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
e.setName("Amar");
Console.WriteLine(e.getId());
Console.WriteLine(e.getName());
}
}
Access Modifiers
Access Modifiers:
Access modifiers are used to set permissions to access the Class and its members
(variables, methods & constructors).
C# supports 4 access modifiers
o private
o protected
o public
o internal
o protected internal
Public: The public keyword allows its members to be visible from anywhere inside the project.
Private: The private members can only be accessed by the member within the same class.
Protected: Protected accessibility allows the member to be accessed from within the class and
from another class that inherits this class.
Internal: Internal provides accessibility from within the project. Another similar internal
accessibility is protected internal. This allows the same as the internal and the only difference is
that a child class can inherit this class and reach its members even from another project.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Encapsulation:
The concept of protecting the data with in the class itself.
We implement Encapsulation through POJO rules
POCO – Plain Old CLR Object
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
AccessEmployee.cs:
public class AccessEmployee{
public static void Main() {
Employee e = new Employee();
e.setNum(101);
e.setName("Amar");
e.setSalary(35000);
Console.WriteLine("Emp Num : "+e.getNum());
Console.WriteLine("Emp Name : "+ e.getName());
Console.WriteLine("Emp Salary : "+e.getSalary());
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Constructor Chaining:
Invoking constructor from another constructor is called Chaining.
this() method is used to invoke constructor.
Types of Inheritance:
1. Single Inheritance
2. Multi-Level Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
class A{
…..
}
class B : A{
…..
}
class A{
....
}
class B : A{
....
}
class C : B{
....
}
class A{
....
}
class B : A{
....
}
class C : A{
....
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
class A{
....
}
class B{
....
}
class C : A, B{
....
}
Interface A{
}
Interface B{
}
Class C : A, B{
}
Interface A{
}
Interface B{
}
Interface C : A, B{
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Single Inheritance: By instantiating child class, we can access both Parent & Child
functionality.
using System;
class Employee {
public void doWork(){
Console.WriteLine("Employee do work");
}
}
class Manager : Employee {
public void monitorWork(){
Console.WriteLine("Manage do work as well as monitor others work");
}
}
public class Company {
public static void Main() {
Manager m = new Manager();
m.doWork();
m.monitorWork();
}
}
In Object creation, Parent object creates first to inherit properties into Child.
We can check this creation process by defining constructors in Parent and Child.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
using System;
class Parent{
public Parent(){
Console.WriteLine("Parent instantiated");
}
}
class Child : Parent{
public Child(){
Console.WriteLine("Child instantiated");
}
}
public class Inherit {
public static void Main(){
new Child();
}
}
this this()
A reference variable used to invoke instance It is used to invoke the constructor of same
members. class.
It must be used inside instance method or It must be used inside the constructor.
instance block or constructor.
base base()
A reference variable used to invoke instance It is used to invoke the constructor of same
members of Parent class from Child class. class.
It must be used inside instance method or It must be used inside Child class constructor.
instance block or constructor of Child class.
Method overriding:
If derived class defines same method as defined in its base class, it is known as method
overriding in C#.
It is used to achieve runtime polymorphism.
It enables you to provide specific implementation of the method which is already
provided by its base class.
To perform method overriding in C#, you need to use virtual keyword with base class
method and override keyword with derived class method
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
using System;
class Parent{
public virtual void fun(){
Console.WriteLine("Parent functionality");
}
}
class Child : Parent{
public override void fun(){
Console.WriteLine("Child functionality");
}
}
public class Inherit {
public static void Main(){
Parent p = new Child();
p.fun();
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
base():
In inheritance, we always create Object to Child class.
In Child object creation process, we initialize instance variables of by invoking Parent
constructor from Child constructor using base().
using System;
class Parent
{
public int a, b;
public Parent(int a, int b)
{
this.a = a;
this.b = b;
}
}
class Child : Parent
{
public int c, d;
public Child(int a, int b, int c, int d) : base(a, b)
{
this.c = c;
this.d = d;
}
public void details()
{
Console.WriteLine("Parent a : " + base.a);
Console.WriteLine("Parent b : " + base.b);
Console.WriteLine("Child c : " + this.c);
Console.WriteLine("Child d : " + this.d);
}
}
public class Inherit
{
public static void Main()
{
Child c = new Child(10, 20, 30, 40);
c.details();
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Polymorphism:
Polymorphism is the concept where object behaves differently in different situations.
using System;
class Calculator {
public void add(int x, int y) {
int sum = x+y;
Console.WriteLine("Sum of 2 numbers is : " + sum);
}
public void add(int x, int y, int z)
{
int sum = x+y+z;
Console.WriteLine("Sum of 3 numbers is : " + sum);
}
}
public class Overload{
public static void Main()
{
Calculator calc = new Calculator();
calc.add(10, 20);
calc.add(10, 20, 30);
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
WriteLine() method is pre-defined and overloaded. Hence it can print any type of data:
using System;
public class Overload
{
public static void Main()
{
Console.WriteLine(10);
Console.WriteLine(23.45);
Console.WriteLine("C#");
}
}
Runtime polymorphism:
Runtime Polymorphism is a Method overriding technique.
Defining a method in the Child class with the same name and same signature of its
Parent class.
We can implement Method overriding only in Parent-Child (Is-A) relation.
Child object shows the functionality(behavior) of Parent and Child is called Polymorphism
using System;
class Parent{
public virtual void behave(){
Console.WriteLine("Parent behavior");
}
}
class Child : Parent{
public override void behave(){
Console.WriteLine("Child behavior");
}
public void behavior(){
base.behave();
this.behave();
}
}
public class Overrriding{
public static void Main(){
Child child = new Child();
child.behavior();
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Object Up-casting:
We can store the address of Child class into Parent type reference variable.
Using parent address reference variable, we can access the functionality of Child class.
using System;
class Parent {
public virtual void fun(){
Console.WriteLine("Parent's functionality");
}
}
class Child : Parent{
public override void fun(){
Console.WriteLine("Updated in Child");
}
}
public class Upcast {
public static void Main() {
Parent addr = new Child();
addr.fun();
}
}
Down casting: The concept of collecting Child object address back to Child type reference
variable from Parent type.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Sealed Classes
sealed:
sealed is a keyword/modifier.
A member become constant if we define it is sealed hence cannot be modified.
We can apply sealed modifier to Class or Method or Variable.
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Abstraction
Abstraction:
Abstraction is a concept of hiding implementations and shows required functionality.
Abstraction describes "What an object can do instead how it does it?".
Abstract Class: Define a class with abstract keyword. Abstract class consists concrete methods
and abstract methods.
Note: We cannot instantiate (create object) to abstract class because it has undefined methods.
abstract class A{
public abstract void m1();
public void m2(){
}
}
public class Access{
public static void Main(){
A obj = new A(); // Error :
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
using System;
public abstract class Parent{
public abstract void m1();
public void m2(){
Console.WriteLine("Parent concrete method");
}
}
public class Child : Parent{
public override void m1(){
Console.WriteLine("Parent abstract method");
}
}
public class Access{
public static void Main(){
Child obj = new Child();
obj.m1();
obj.m2();
}
}
using System;
abstract class Parent{
public int a, b;
public Parent(int a, int b){
this.a = a;
this.b = b;
}
public abstract void details();
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Interfaces
Interface:
Interface allow to define only abstract methods.
Interface methods are ‘public abstract’ by default.
interface Sample {
void m1();
void m2();
}
Implementing an interface:
Any class can implement the interface by overriding the methods of interface.
We override the methods of interface using public modifier.
using System;
interface First {
void m1();
void m2();
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
Upcasting: object reference of implemented class storing into Interface type variable
Relations:
class A{
}
class B{
}
class C : A, B{
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016
C# .Net - Object Oriented Programming
class A{
}
interface B{
}
class C : A, B{
}
interface A{
}
interface B{
}
class C : A, B{
}
using System;
interface A{
void m1();
}
interface B{
void m2();
}
class C : A, B{
public void m1(){
Console.WriteLine("m1...");
}
public void m2(){
Console.WriteLine("m2...");
}
}
public class Multiple
{
public static void Main()
{
C obj = new C();
obj.m1();
obj.m2();
}
}
Ameerpet Technologies, Near Satyam Theatre, Opposite HDFC Bank, Ameerpet, Hyderabad-500016