C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

Java Programming Abstract classes and Interfaces.
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.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
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.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Inheritance and Polymorphism 7. Building Applications Using C# / Session 7 © Aptech Ltd. Objectives  Define and describe inheritance  Explain method.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Computer Science [3] Java Programming II - Laboratory Course Lab 1 + 2: Review inheritance & abstract Review inheritance & abstractPolymorphism Faculty.
ILM Proprietary and Confidential -
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Chapter 10 Inheritance and Polymorphism
Peyman Dodangeh Sharif University of Technology Fall 2014.
Abstract Classes and Interfaces 8. Building Applications Using C#/ Session 8 © Aptech Ltd. Objectives  Define and describe abstract classes  Explain.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
CS 2430 Day 9. Announcements Quiz 2.1 this Friday Program 2 due this Friday at 3pm (grace date Sunday at 10pm)
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Yingcai Xiao Programming and Debugging in Unity Yingcai Xiao.
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.
Interfaces & Abstract Classes (For CS III AP) March 2014.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Programming and Debugging
Inheritance class TwoDShape { private double width;
using System; namespace Demo01 { class Program
Ch 10- Advanced Object-Oriented Programming Features
Building Java Programs
C# - Inheritance and Polymorphism
Advanced Programming in Java
Presented By: Nazia Hossain Lecturer, Stamford University
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Functions Used to write code only once Can use parameters.
Programming II Polymorphism A.AlOsaimi.
Programming and Debugging
class PrintOnetoTen { public static void main(String args[]) {
What about multi-dimensional arrays?
Building Java Programs
Classes: Arrays group many objects of the same type (in order)
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
C++ Object Oriented 1.
Computer Science II for Majors
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

C# - Inheritance 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.

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. Inheritance

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: class {... } 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

Queries?