Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
Chapter 10: Introduction to Inheritance
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
LECTURE 07 Programming using C# Inheritance
Object Oriented Software Development
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.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
An Object-Oriented Approach to Programming Logic and Design
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.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
CSC 142 Computer Science II Zhen Jiang West Chester University
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Introduction to Object-Oriented Programming Lesson 2.
Interfaces and Inner Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Class Inheritance SWE 344 Internet Protocols & Client Server Programming.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
BY:- TOPS Technologies
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
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.
Classes (Part 1) Lecture 3
Lecture 12 Inheritance.
Inheritance and Polymorphism
OOP’S Concepts in C#.Net
The University of Texas Rio Grande Valley
Interface.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Classes & Objects: Examples
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
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.
Fundaments of Game Design
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
CSG2H3 Object Oriented Programming
Presentation transcript:

Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2

Summary of last lecture Hello world recapObjects & Classes? Variables & variable declarations Object Manipulation

What’s to come today? Introduction to Objects Naming classes(Abstract, base and inherited) Reference (object) types Functions of classes (reusability) Attributes to a class

Objects An object is an instance of a class, you can have many different instances of the same class All objects have states and behaviours. An object’s state is determined by it’s fields (variables) An object’s behaviour is determined by it’s methods State-Off Behaviour-No current flowing State-On Behaviour-Current flowing

Inheritance Simply put inheritance is when a class is defined in terms of another class This allows for the reuse of code Inheritance implements an IS-A relationship. E.g A giraffe IS-A mammal. Inheritance is one of the most important concepts of object oriented programming.

Inheritance Example

public class ParentClass{ public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm printing from a Parent Class."); } } public class ChildClass1 : ParentClass { public ChildClass1() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass1 child = new ChildClass1(); child.print(); } } public class ChildClass2 : ParentClass { public ChildClass2() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass2 child = new ChildClass2(); child.print(); } } Both child classes are able to access the parent class’ public method “print()”

Abstract Classes A class that can not be instantiated, only sub classed (inheritance) An abstract class can contain abstract methods An abstract method is a method that has a declaration but has no statements We use an abstract class because we want to require subclasses to have certain methods or variables but want that subclass to fill in the rest of the code

Abstract Classes An abstract class is the only type of class that can contain an abstract method, otherwise the compiler will produce an error If an abstract class has an abstract method, then any class that inherits the abstract class will have to override the abstract method

Abstract Class Example class Program { static void Main(string[] args) { Giraffe raffe1 = new Giraffe(); raffe1.printFeatures(); raffe1.print(); Console.Read(); } abstract class Mammal{ protected int numOfLimbs; public abstract void setFeatures(int numLimbs); public abstract void printFeatures(); public void print(){ Console.WriteLine(“This animal is a mammal"); } class Giraffe : Mammal { public Giraffe() { setFeatures(4); } public override void setFeatures(int numLimbs) { this.numOfLimbs = numLimbs; } public override void printFeatures() { Console.WriteLine("The giraffe has " + numOfLimbs + " legs."); }

Abstract Class Example An abstract class can also contain concrete methods such as the print() method in the previous example Concrete methods can be used so that all sub classes have access to that method The result of our simple program is as follows

Base and Inherited Classes A base class is a class from which other classes are derived from. Code can be implicitly inherited from a base class and can be reused by the child (sub/derived) class. An inherited class is a class that is based on another class. Inheritance allows for the reuse of code from a base class and allows for independent changes to be made

Base Class and Inherited Class Example public class Bird{ protected bool canFly; protected void setCanFly(bool cFly) { canFly = cFly; } public void message(){ if (this.canFly){ Console.WriteLine("This bird can fly"); } else{ Console.WriteLine("This bird cannot fly"); } } } public class Robin: Bird { public Robin(){ setCanFly(true); } } class birdTest{ static void Main(string[] args) { Robin robin1 = new Robin(); robin1.message(); Console.Read(); } Parent Class Child Class

Reference Types A reference type is a data type that stores a reference to it’s data instead of storing the data’s value. So a reference variable is stored and the actual data is stored This means different variables can reference a single piece of data. Since a single piece of data can be accessed by different variables. Modifying one variable may affect another. References are like labels for objects You can change and move the labels around but they always refer to the one object Examples of reference types in C#; -Classes -Interfaces -Delegates

Example using Classes Name = john; john = new Name(); Name mark = john; Variable Name – is a reference to the Name object Name is a class Memory allocates two different spaces; one for the data and one for the reference to that data Only one space is made available for the reference to the data to be copied into. Name Object john mark

Methods and References By default methods pass parameters by value in C# i.e. it will pass either a copy of the value or a copy of the reference depending on whether the parameter is a value or reference type. Parameters can be passed by reference instead of value by using the ref keyword foo(ref string eyeColour) { Console.WriteLine(“This person has” + eyeColour+” eyes.”); } ….. foo(ref string eyeColour); //method call

Functions of Classes The use of classes in object oriented programming allows for great reusability purposes. Classes usually do one specific thing and should be named to suit it’s purpose Once we have written a class we can access that class and use all of it’s methods and functionality To do this we need to make sure we have used the “using” keyword or that the class is in the namespace we are using

Functions of Classes Once we can use the class outside of itself, there is great potential for reusability It is possible to call methods from that class and use their functionality without having to rewrite all the code For instance if you are looking to create a program to calculate the price of travelling by car to three different places. You could write three different classes to calculate each distance or you could write one class that would allow for three objects to be created which could store the price.

Attributes An attribute is a special tag in a C# class It stores metadata about objects, methods or properties An attribute is usually a property of a property An attribute is marked by being enclosed by [] There are many predefined attributes available but it also possible to make custom attributes

Attribute Example #define DEBUG using System; using System.Diagnostics; public class Myclass { [Conditional("DEBUG")] public static void Message(string msg) { Console.WriteLine(msg); } } class Test { static void function1() { Myclass.Message("In Function 1."); function2(); } static void function2() { Myclass.Message("In Function 2."); } public static void Main() { Myclass.Message("In Main function."); function1(); Console.ReadKey(); } } Sets DEBUG to true if in debug mode Predefined Attribute checks if DEBUG is true Calls on the attribute class’ method The messages will only be printed if the compiler is in debug mode

What did we cover today? Introduction to Objects Naming classes(Abstract, base and inherited) Reference (object) types Functions of classes (reusability) Attributes to a class

What’s to come next time Principles of control structures in terms of sequence and selection