CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Object Oriented Programming with Java
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
IEG 3080 Tutorial 2 Jack Chan. Prepared by Jack Chan, Spring 2007 Outline.Net Platform Object Oriented Concepts Encapsulation Inheritance Polymorphism.
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.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Object-Oriented Programming Polymorphism Session 07 Mata kuliah: M0874 – Programming II Tahun: 2010.
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.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Composition Reusing the code and functionality Unit - 04.
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.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Section 3: Component/Object Oriented Programming, Events, and Delegates in.NET Jim Fiddelke, Greenbrier & Russel.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
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.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Chapter 2 Objects and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes and Inheritance
Inheritance and Polymorphism
Methods Attributes Method Modifiers ‘static’
CS360 Windows Programming
The University of Texas Rio Grande Valley
Abstract Classes AKEEL AHMED.
Inherited Classes in Java
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
Understanding Polymorphism
CIS 199 Final Review.
Presentation transcript:

CIS 3301 C# Lesson 7 Introduction to Classes

CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand Destructors. Familiarization with Class Members.

CIS 3303 Classes Classes are declared by using the keyword class followed by the class name and a set of class members surrounded by curly braces Every class has a constructor that initializes class members when the class is created Constructors do not have return values and always have the same name as the class

CIS 3304 Classes (cont) class OutputClass { string myString; // Constructor public OutputClass(string inputString) { myString = inputString; } // Instance Method public void printString() { Console.WriteLine("{0}", myString); } // Destructor ~OutputClass() { // Some resource cleanup routines } }

CIS 3305 Classes (cont) In C#, there are two types of class members –Instance class members belong to a specific occurrence of a class –Static members can be accessed simply by using the syntax.. There is only ever one copy of a static class member. Use static constructor to initialize static fields in a class Destructors look just like constructors, except they start with a tilde, "~". They are normally called by the C# garbage collector.

CIS 3306 Class Member Types Constructors Destructors Fields Methods Properties Indexers Delegates Events Nested Classes

CIS 3307 C# Lesson 8 Class Inheritance

CIS 3308 Objectives Implement Base Classes. Implement Derived Classes. Initialize Base Classes from Derived Classes. Learn How to Call Base Class Members. Learn How to Hide Base Class Members.

CIS 3309 Inheritance Example public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); } }

CIS Inheritance Can create a child class ChildClass, using existing code from ParentClass This is accomplished through the ChildClass declaration –public class ChildClass : ParentClass C# supports single class inheritance only. However, it does allow multiple interface inheritance ChildClass has exactly the same capabilities as ParentClass

CIS Inheritance (cont) Can access base class members –prefixing the method name with "base.“ –through an explicit cast ((Parent)child).print(); The new modifier on a Child class enables this method to hide the Parent class method, thus explicitly preventing polymorphism

CIS C# Lesson 9 Polymorphism

CIS Objectives Learn What Polymorphism Is. Implement a Virtual Method. Override a Virtual Method. Use Polymorphism in a Program.

CIS Polymorphism Example public class DrawingObject { public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } public class Circle : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Circle."); } }

CIS Polymorphism Implemented using System; public class DrawDemo { public static int Main( ) { DrawingObject[] dObj = new DrawingObject[4]; dObj[0] = new Line(); dObj[1] = new Circle(); dObj[2] = new Square(); dObj[3] = new DrawingObject(); foreach (DrawingObject drawObj in dObj) { drawObj.Draw(); } return 0; } }

CIS Summary Polymorphism. It allows you to implement derived class methods through a base class pointer during run-time Implemented a derived class method that overrides a virtual method