- Procedural oriented approach
- Object oriented approach
In procedural oriented approach, we have a set of procedures or functions to execute a piece of code or to perform some functionality. A programming language, which follows Procedural oriented approach is said to be procedural oriented programming language. In this approach, program is divided into various parts called a function/method. Procedural oriented approach follows top down approach. It doesn’t contain any kind of access modifiers like public, private, internal etc..
As these are written in multiple function/method, we cannot perform the method hiding due to which we can’t provide security for procedural oriented approach. It doesn’t support function/method overloading.
Examples of procedural oriented approach are : C, VB , Pascal etc.
Object oriented approach
In Object oriented approach, we have a collection of classes and an object.
A programming language, which follows object oriented approach is said to be object oriented programming language. In this approach, a program is divided into multiple parts called classes/objects. Object oriented approach follows bottom up approach, as we invoke after creating the object. It supports the access modifiers to a class and its members like variable or a method/property/constructor etc. It supports method due to which OOPS provides security in object oriented approach. It supports method overloading etc. The examples of object oriented approach are C++ , JAVA ,C# ,VB.NET etc. To achieve the above object oriented approach, we will the use classes and objects concept.
Class
Class is a collection of the data members and member functions.
The data member/variable/state is used to represent some value.
Example
String EmployeeName=”khaja moiz”;
The member function/method/behavior is used to perform the functionality like for calculating an employee salary etc.
Example
Syntax for creating an object/accessing an instance class
- <classname> reference= new <classname>();
- reference.Methodname();
- <classname>.StaticMethodName();
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Encapsulation in OOPS is wrapping of the states/variables/data members and behaviours/methods/member functions in a particular class.
(or)
It is a combination of methods and the variables or the properties in a single container is nothing but a class.
Example to understand Encapsulation
- Using system;
- Namespace Encapsulation Example {
- class EmployeeClass {
- int EmployeeNo;
- string EmployeeName;
- Public void DisplayDetails() {
- Console.WriteLine(Employee Number is: “+ EmployeeNo);
- Console.WriteLine(Employee Name is: “+ EmployeeName);
- }
- }
- }
- Data Abstraction
- Method Abstraction
It is a process of hiding the data, which is unwanted or unnecessary and showing only the relevant data to the user.
Example to understand Data Abstraction
- Using system;
- namespace data abstraction example {
- class Employee {
- ulong EmployeeNo;
- string EmployeeName;
- double EmployeeSalary;
- String EmployeeDesignation;
- Internal Employee(ulong EmployeeNo, string EmployeeName, double EmployeeSalary, string EmployeeDesignation) {
- this.EmployeeNo = EmployeeNo;
- this.EmployeeName = EmployeeName;
- this.EmployeeSalary = Employeesalary;
- this.EmployeeDesignation = EmployeeDesignation;
- }
- Internal void DisplayEmployDetails() {
- Console.writeline(“EmployeeNo is :” + EmployeeNo);
- console.writeline(“EmployeeName is :” + EmployeeName);
- console.writeline(“Employeesalary is :” + Employeesalary);
- console.writeline(“Employeedesignation is :” + Employeedesignation);
- }
- internal void Hike() {
- // In this hike method we required employee details like EmployeeNo , EmployeeName and Employee Salary. EmployeeDesignation
- is an unwanted data
- for Hike Method.
- }
- class program {
- static void main() {
- Employee emp = new Employee(1026, “khajaMoiz”, 2000, “Software Developer”);
- emp.DisplayEmployDetails();
- emp.Hike();
- Console.Readline();
- }
- }
- }
- }
Example to understand Method Abstraction
- Using system;
- namespace Method abstraction Example {
- class Employee {
- public void EmployeeNo(long EmployeeNo) {
- console.writeline(“EmployeeNo is :” + EmployeeNo);
- }
- public void EmployeeName(string EmployeeName) {
- console.writeline(“EmployeeName is :” + EmployeeName);
- }
- public void EmployeeSalary(double EmployeeSalary) {
- console.writeline(“Employeesalary is :” + Employeesalary);
- }
- public void EmployeeDesignation(string EmployeeDesignation) {
- console.writeline(“EmployeeDesignation is :” + EmployeeDesignation);
- }
- }
- class program {
- Employee emp = new Employee();
- emp.EmployeeName(“khaja Moizuddin”);
- emp.EmployeeSalary(2000);
- Console.Readline();
- }
- }
- emp.EmployeeName(“khaja Moizuddin”);
- emp.EmployeeSalary(2000);
A class from which we are inheriting is known as super class and a class, where we are receiving/accessing is called a sub class. Super class can also be called as a parent class or a base class. Sub class can also be called as a child class or a derived class. Using inheritance, we can access all the members from the super class/parent class/base class to the derived class/child class/sub class directly without creating the object or without using an objectname or a classname, we can access those members directly.
It supports re usability of the classes and its members.
- Single inheritance
- Multi level inheritance
- Multiple inheritance
- Hybrid inheritance
- Hierarchical inheritance.
Inheriting/accessing the members from one class to another class is called single inheritance.
Syntax for Single inheritance
- class Employee {}
- class Organisation: Employee {}
Example to understand Single inheritance
- using system;
- namespace Single inheritance Example
- class Employee {
- int employeeno;
- string employeename;
- double employeesalary;
- void EmployeeDetails() {
- console.writeLine(“EmployeeNo is :” + EmployeeNo);
- console.writeLine(“EmployeeName is :” + EmployeeName);
- console.writeLine(“EmployeeSalary is :” + EmployeeSalary);
- }
- }
- class Organisation: Employee {
- string CompanyName;
- string CompanyAddress;
- int Pincode;
- void OrganisationDetails() {
- EmployeeDetails();
- }
- }
Multi Level inheritance
Inheriting /accessing the members of one super class to its derived class and again accessing the members of this derived class from other derived class is called multi level inheritance.
Syntax for multi level inheritance
- class Employee {}
- class organisation: Employee {}
- class Events: organisation {}
- using system;
- namespace multi level inheritance {
- class Branch {
- int branchid;
- string branchname;
- string branchlocation;
- internal Branch(int branchid, string branchname, string branchlocation) {
- this.branchid = branchid;
- this.branchname = branchname;
- this.branchlocation = branchlocation;
- }
- internal void BranchDetails() {
- console.writeline(“Branch id is :” + branchid);
- console.writeline(“Branch name is :” + branchname);
- console.writeline(“Branch location is :” + branchlocation);
- }
- }
- class Employee: Branch {
- int EmployeeNo;
- string EmployeeName;
- internal Employee(int EmployeeNo, string EmployeeName): Base(1077, “SBI”, “Hyderabad”) {
- this.EmployeeNo = EmployeeNo;
- this.EmployeeName = EmployeeName;
- }
- internal void EmployeeDetails() {
- console.writeline(“Employee Number is :” + EmployeeNo);
- console.writeline(“Employee Name is:” + EmployeeName);
- }
- }
- class Salary: Employee {
- double basic;
- double hra;
- double da;
- double gross;
- internal Salary(double basic): base(1066, “khajaMoiz”) {
- this.basic = basic;
- this.hra = hra;
- this.da = da;
- this.gross = gross;
- }
- internal void SalaryDetails() {
- hra = 0.4 * basic;
- da = 0.2 * basic;
- gross = hra + da + basic;
- console.writeline(“Basic salary is:” + basic);
- console.writeline(“hra is:” + hra);
- console.writeline(“da is:” + da);
- console.writeline(“gross is:” + gross);
- }
- }
- class program {
- static void main() {
- Salary objref = new Salary(12000);
- objref.SalaryDetails();
- Console.Readline();
- }
- }
- }
C# doesn’t support multiple inheritance by using classes, which will support only by using interfaces.
Syntax for Multiple Inheritance
- Interface IEmployee {}
- Interface IOrganisation {}
- class Events: IEmployee, IOrganisation {}
- using system;
- namespace Multiple Inheritance Example {
- interface IBasicMobileFeatures {
- void Calls();
- void alarmclock();
- void calculator();
- }
- class Iphone: IBasicMobileFeatures {
- public void Calls() {
- console.writeline(“Iphone calls”);
- }
- public void alarmclock() {
- console.writeline(“Iphone alarmclock”);
- }
- public void calculator() {
- console.writeline(“Iphone calculator”);
- }
- }
- class Blackberry: IBasicMobileFeatures {
- public void Calls() {
- console.writeline(“Blackberry calls”);
- }
- public void alarmclock() {
- console.writeline(“Blackberry alarm clock”);
- }
- public void calculator() {
- console.writeline(“Blackberry calculator”);
- }
- }
- interface ISmartphoneDevices {
- void WIFI();
- void Youtube();
- void Whatsapp();
- }
- class Samsung: IBasicMobileFeatures, ISmartphoneDevices // Implementing multiple inheritance.
- {
- public void calls() {
- console.writeline(“Samsung calls”);
- }
- public void alarmclock() {
- console.writeline(“Samsung alarmclock”);
- }
- public void calculator() {
- console.writeline(“Samsung calculator”);
- }
- public void WIFI() {
- console.writeline(“Samsung’s WIFI”);
- }
- public void Youtube() {
- console.writeline(“Samsung’s youtube”);
- }
- public void Whatsapp() {
- console.writeline(“samsung’s Whatsapp”);
- }
- }
- class program {
- IBasicMobileFeatures objref = new Iphone();
- objref.calls();
- objref.alarmclock();
- objref.calculator();
- objref = new Blackberry();
- objref.calls();
- objref = alarmclock();
- objref.calculator();
- ISmartphoneDevices objref2 = (ISmartphoneDevices) objref1;
- objref2.WIFI();
- objref2.Youtube();
- objref2.Whatsapp();
- Console.Readline();
- }
- }
Syntax for Hierarchical inheritance
- class Employee {}
- class Organisation: Employee {}
- class Events: Employee {}
A combination of Singe inheritance and a multi level inheritance
There are two types of Polymorphism, which are given below.
- Static Polymorphism or Function Overloading
- Dynamic Polymorphism or Function Overriding.
The method parameter or method signature depends on the components given below.
- Number of parameters.
- Type of parameters
- Order of parameters.
In parent class /base class, we will use a virtual keyword and in the derived class, we will use override keyword. To implement function overriding, we will create a super class reference variable and a sub class object. We cannot implement dynamic polymorphism in a single class.
Function Overriding depends on the things given below.
- Method name should be same.
- Signature/parameters should be same.
- Return type should be same.
- Access Modifier should be same.