C# - Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Object Oriented Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Exercises on Basic OOP TCP1201: 2013/2014. Catch the Bug 1 class Point { private : int x, y; public : Point(int u, int v) : x(u), y(v) { } int getX()
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Inheritance, Polymorphism, and Virtual Functions
Inheritance and Polymorphism 7. Building Applications Using C# / Session 7 © Aptech Ltd. Objectives  Define and describe inheritance  Explain method.
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.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Chapter 10 Inheritance and Polymorphism
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter -6 Polymorphism
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance class TwoDShape { private double width;
Ch 10- Advanced Object-Oriented Programming Features
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
Polymorphism & Virtual Functions
Presented By: Nazia Hossain Lecturer, Stamford University
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Modern Programming Tools And Techniques-I Inheritance
OOP’S Concepts in C#.Net
Learning Objectives Inheritance Virtual Function.
Inheritance, Polymorphism, and Virtual Functions
Week 6 Object-Oriented Programming (2): Polymorphism
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
What about multi-dimensional arrays?
Lecture 10 Concepts of Programming Languages
Class: Special Topics Overloading (methods) Copy Constructors
TOPIC: FUNCTION OVERLOADING
Chapter 11 Class Inheritance
C++ Object Oriented 1.
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Inheritance and Polymorphism
Presentation transcript:

C# - Inheritance and Polymorphism Ashima Wadhwa

Inheritance One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.

Inheritance When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

Base and Derived Classes A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces. The syntax used in C# for creating derived classes is as follows: <acess-specifier> class <base_class> { ... } class <derived_class> : <base_class> { ...

using System; namespace InheritanceApplication { class Shape public void setWidth(int w) width = w; } public void setHeight(int h) height = h; protected int width; protected int height;

// Derived class class Rectangle: Shape { public int getArea() return (width * height); } class RectangleTester static void Main(string[] args) Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. Console.WriteLine("Total area: {0}", Rect.getArea()); Console.ReadKey();

class Shape {     public double Width;     public double Height;     public void ShowDim()     {         Console.WriteLine("Width and height are " +         Width + " and " + Height);     } } class Triangle : Shape     public string Style;         public double Area()         return Width * Height / 2;         public void ShowStyle()         Console.WriteLine("Triangle is " + Style);

class Driver { static void Main() Triangle t1 new Triangle(); Triangle t2 new Triangle(); t1.Width =4.0; t1.Height =4.0; t1.Style ="isosceles"; t2.Width =8.0; t2.Height =12.0; t2.Style ="right"; Console.WriteLine("Info for t1: "); t1.ShowStyle(); t1.ShowDim(); Console.WriteLine("Area is " + t1.Area()); Console.WriteLine(); Console.WriteLine("Info for t2: "); t2.ShowStyle(); t2.ShowDim(); Console.WriteLine("Area is " + t2.Area()); }

The output from this program is shown here: Info for t1: Triangle is isosceles Width and height are 4 and 4 Area is 8 Info for t2: Triangle is right Width and height are 8 and 12 Area is 48

Polymorphism The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'. Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

Static Polymorphism The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. They are: Function overloading Operator overloading

Function Overloading You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type. The following example shows using function print() to print different data types:

using System; namespace PolymorphismApplication { class Printdata void print(int i) Console.WriteLine("Printing int: {0}", i ); } void print(double f) Console.WriteLine("Printing float: {0}" , f); void print(string s) Console.WriteLine("Printing string: {0}", s); static void Main(string[] args) Printdata p = new Printdata(); // Call print to print integer p.print(5); // Call print to print float p.print(500.263); // Call print to print string p.print("Hello C++"); Console.ReadKey();

Output Printing int: 5 Printing float: 500.263 Printing string: Hello C++

Queries?