OOP’S Concepts in C#.Net

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Advanced Object-Oriented Programming Features
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Classes, Interfaces and Packages
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
BY:- TOPS Technologies
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Chapter - 4 OOP with C# S. Nandagopalan.
OOP: Encapsulation &Abstraction
Lecture 12 Inheritance.
Objects as a programming concept
Ch 10- Advanced Object-Oriented Programming Features
2.7 Inheritance Types of inheritance
Final and Abstract Classes
Inheritance and Polymorphism
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Modern Programming Tools And Techniques-I Inheritance
Inheritance Basics Programming with Inheritance
Interface.
Java Programming Language
Conditional Statements
Abstract Classes AKEEL AHMED.
Interfaces.
METHOD OVERRIDING in JAVA
Computer Programming with JAVA
Java – Inheritance.
Inheritance Inheritance is a fundamental Object Oriented concept
Java Programming, Second Edition
Chapter 10: Method Overriding and method Overloading
Fundaments of Game Design
Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Inheritance and Polymorphism
CIS 199 Final Review.
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

OOP’S Concepts in C#.Net

Four Basic concepts Abstraction Encapsulation Polymorphism Inheritance

Abstraction Abstraction is of two types:- 1.Abstract Class:- -class is not instantiated -new class() causes compiler error e.g. abtsract public class employee { string Name; void printName(); }

Contd.. Abstract methods -methods have no implementation -methods are automatically virtual -class are automatically virtual

using System; namespace OOPs {           /// Abstract class      abstract class Abstract1      {           public Abstract1()           {                // TODO: Add constructor logic here           }            //non-abstract method            public void nonabmethod()      {           System.Console.WriteLine("Non Abstract Method!");      }           //abstract method           public abstract void abmethod(); //they are implicitly virtual it is not contain the body of the method           public static void Main()           {                myclass mycls = new myclass();                mycls.nonabmethod();                mycls.abmethod();           }      }

Contd… class myclass : Abstract1 //class derived from abstract class           {                public override void abmethod() //must implement the abstract method derived in class           {                System.Console.WriteLine("Abstract Method!");           }      } }

Encapsulation Encapsulation is the procedure of covering up of data and functions into a single unit (called class). By using this, Encapsulation provides a way to protect data from accidental corruption. In java,there are get() and set() methods are available same like that in C# accessor and mutator methods are used for encapsulation.

Example of using accessor and mutator using system; public class Department { private string departname; ....... // Accessor. public string GetDepartname() { return departname; } // Mutator. public void SetDepartname( string a) { departname=a; } }

Contd. public static int Main(string[] args) { Department d = new Department(); d.SetDepartname("ELECTRONICS"); Console.WriteLine("The Department is :"+d.GetDepartname()); return 0; }

By using properties also we can achieve encapsulation in C#.net Properties are a new language feature introduced with C# Properties in C# helps in protect a field in a class by reading and writing to it The benefit of properties is that the users of your objects are able to manipulate the internal data point using a single named item.

Example using system; public class Department { private string departname; public string Departname { get { return departname; } set { departname=value; } } }

public class Departmentmain { public static int Main(string[] args) { Department d= new Department(); d.Departname="Communication"; Console.WriteLine("The Department is :{0}",d.Departname); return 0; } } Thus we are manipulate our read and write both methods of class This method has another two types:- Read only property Write only property

READ ONLY PROPERTY: using system; public class ReadDepartment { private string departname; public ReadDepartment(string avalue) { departname=avalue; } public string Departname { get { return departname; } } }

public class ReadDepartmain { public static int Main(string[] args) { ReadDepartment d= new ReadDepartment("COMPUTERSCIENCE"); Console.WriteLine("The Department is: {0}",d.Departname); return 0; } }

WRITE ONLY PROPERTY: using system; public class WriteDepartment { private string departname; public string Departname { set { departname=value; Console.WriteLine("The Department is :{0}",departname); } } }

public class WriteDepartmain { public static int Main(string[] args) { WriteDepartment d= new WriteDepartment(); d.departname="COMPUTERSCIENCE"; return 0; } }

Inheritance Inheritance is the process of creation new classes from already existing classes C# supports two types of Inheritance mechanisms :- Implementation Inheritance Interface Inheritance

What is Implementation Inheritance? When a class (type) is derived from another class(type) such that it inherits all the members of the base type it is Implementation Inheritance.

What is Interface Inheritance? When a type (class or a struct) inherits only the signatures of the functions from another type it is Interface Inheritance

Contd.. By using ‘abstract ’and ‘interface’ keywords in base class we can inherit that class C# does not support multiple implementation inheritance which is supported by C++ but it supports multiple interface inheritance

super v/s base In java if the constructor of base class is called in derived class ‘super’ keyword is used Thus in C# .’base ’keyword is used With help of ‘base ‘,we can call method of base class in derived class

BUT? If we don’t want that then? In C#,there will be facility of Sealed classes Once a class is defined as sealed class, this class cannot be inherited. The sealed modifier is used to define a class as sealed.This is very useful when the classes have static members.

Polymorphism Polymorphism means same operation may behave differently on different classes. Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism.

Types Of Polymorphism Compile Time Polymorphism Method Overloading Run Time Polymorphism: Method Overriding

Method Overloading Method with same name but with different arguments is called method overloading. E.g class A1 { void hello() { Console.WriteLine(“Hello”); } void hello(string s) { Console.WriteLine(“Hello {0}”,s); } }

Method Overriding Method overriding occurs when child class declares a method that has the same type arguments as a method declared by its superclass. E.g Class parent { virtual void hello() { Console.WriteLine(“Hello from Parent”); } }

Class child : parent { override void hello() { Console Class child : parent { override void hello() { Console.WriteLine(“Hello from Child”); } } static void main() { parent objParent = new child(); objParent.hello(); }

Virtual methods In C#,for method overriding ‘virtual’ keyword is used If in base class,method is virtual then it is overridden by derived class by using ‘overrides’

Shadow implementation If in child class any method has modifier ‘new’ which is of base class then it is called ‘shadow implementation’ It means method is not overriding

Example class Animal { public Animal() { Console.WriteLine("Animal constructor"); } public void Greet() Console.WriteLine("Animal says Hello"); public void Talk() Console.WriteLine("Animal talk"); public virtual void Sing() Console.WriteLine("Animal song");

class Dog : Animal { public Dog() Console.WriteLine("Dog constructor"); } public new void Talk() Console.WriteLine("Dog talk"); public override void Sing() Console.WriteLine("Dog song"); };

Contd.. Animal a2 = new Dog(); a2.Talk(); a2.Sing(); a2.Greet(); //Output Animal constructor Dog constructor Animal talk Dog song Animal says Hello