1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
Object Oriented Concepts
Object Oriented Concepts Classes II. Object Oriented concepts Encapsulation Composition Inheritance Polymorphism.
Lecture 9 Polymorphism Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Inheritance and Access Control CS 162 (Summer 2009)
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Introduction to Object-Oriented Programming Lesson 2.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
OOP: Encapsulation &Abstraction
Object-oriented Programming in Java
Lecture 07 C# - Inheritance and Polymorphism Dr. Eng. Ibrahim El-Nahry.
Inheritance and Polymorphism
OOP’S Concepts in C#.Net
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Polymorphism and access control
Abstract Classes AKEEL AHMED.
METHOD OVERRIDING in JAVA
Java Programming, Second Edition
Java Inheritance.
Fundaments of Game Design
CIS 199 Final Review.
Lecture 10 Concepts of Programming Languages
C++ Object Oriented 1.
Presentation transcript:

1 C# - Inheritance and Polymorphism

2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The Sealed Keyword 6.Object Class 7.Polymorphism 8.Overriding Methods 9.Keywords 10.Boxing and Un-boxing Course Topics

3 Inheritance  Reusability in Object Oriented Programming languages is achieved by reducing coupling between different classes, while extensibility is achieved by sub-classing.  The process of sub-classing a class to extend its functionality is called Inheritance or sub-typing.  The original class (or the class that is sub-typed) is called the base, parent or super class. The class that inherits the functionality of the base class and extends it in its own way is called the sub, child, derived or inherited class.

4 Implementing Inheritance in C#  C# uses the colon ':' operator to indicate inheritance. Sample code

5 Constructor calls in Inheritance  When we instantiate the sub-class, the compiler first instantiates the base-class by calling one of its constructors and then calling the constructor of the sub-class.

6 Calling Constructors of Base Class  We can explicitly call the constructor of a base-class using the keyword base. base must be used with the constructor header (or signature or declaration) after a colon ':' operator

7 Types Protected Access Modifier  C# provides the protected access modifier to protect the data.  Protected members of the class can be accessed either inside the containing class or inside its sub-class.  Users still won't be able to call the protected members through object references and if one tries to do so, the compiler will generate an error.

8 Protected Internal Access Modifier In a similar way, the protected internal access modifier allows a member (field, property and method) to be accessed:  Inside the containing class, or  Inside the same project, or  Inside the sub-class of the containing class.  Hence, protected internal acts like 'protected OR internal', i.e., either protected or internal.

9 Contd…

10 The sealed keyword  Finally, if you don't want your class to be inherited by any class, you can mark it with the sealed keyword.  No class can inherit from a sealed class. Sample Code

11 Object Class  In C# all the types (classes, structures and interfaces) are implicitly inherited from the Object class defined in the System namespace.  This class provides the low- level services and general methods to each and every class in the.NET framework.  The Object class is extremely useful when it comes to polymorphism, as a reference of type Object can hold any type of object. Object Class Methods  Equals  Static Equals  GetHashCode  GetType  Static ReferenceEquals  ToString  ProtectedFinalize  ProtectedMemberwiseClone

12 Polymorphism  Polymorphism is the ability for classes to provide different implementations of methods that are called by the same name.  Polymorphism allows a method of a class to be called without regard to what specific implementation it provides.

13 Method Overriding  Virtual and Override keywords allows you to implement Methods Overriding in Base and Derived Classes.  Different implementations of a method with the same name and signature in the base and sub-classes is called as Polymorphism. Sample code

14 The new keyword  C# introduces a keyword new to mark a method as a non- overriding method and as the one which we don't want to use polymorphically.

15 “is” and “as” keywords  To check the run-time type of an object, you can use either “is” or “as” keyword.  “is” compares the type of the object with the given type and returns true if it is cast-able otherwise, it returns false.

16 Contd…

17 Contd… “as”

18 Boxing  Boxing a value type allocates an object instance on the heap and copies the value into the new object.  Boxing is an implicit conversion of a Value Types to the type object or to any interface type implemented by this value type.  Boxing is used to store value types in the garbage-collected heap.  It is called automatically when the object is about to be destructed. * Source from MSDN

19 Un-boxing  Un-boxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An un-boxing operation consists of:  Checking the object instance to make sure it is a boxed value of the given value type.  Copying the value from the instance into the value-type variable. * Source from MSDN

20 Contd…

21 Exercises 1.Create a Class with Name Person (Base Class). Create another class with the name Employee (Derived Class). Inherit the Person Class into Employee Class. 2.Implement the following things in the program for both classes. a) Write default Constructors b) Overload the Constructors c) Implement Overriding concept d) Call base class methods inside Derived Class e) Create the instance of Derived Class in the Main() to access the methods f) De-allocate the memory manually g) Work with Access Specifiers according to requirement 3.Create the instance of BaseClass and check the output of the program. 4.Operator (+,/,-,*) overloading implementation to perform operations on objects. 5.Implement the following and check out the output of the program. You need to implement Console.WriteLine in each class constructors. Create a Base class called B. Create a class call D derived from B. Create a Class called C, in constructor instantiate Class D. In main function of the program instantiate class C

22 References URL : us/vbcn7/html/vaconinheritanceforbasiclanguage.asp