Methods Attributes Method Modifiers ‘static’

Slides:



Advertisements
Similar presentations
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.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
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.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
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 –
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance and Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
C# for C++ Programmers 1.
Advanced Programming in Java
Examples of Classes & Objects
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CIS 200 Test 01 Review.
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
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.
Programming Language Concepts (CIS 635)
The fattest knight at King Arthur's round table was Sir Cumference
Object Oriented Programming
C# In many ways, C# looks similar to Java
Lecture 23 Polymorphism Richard Gesick.
Overloading and Constructors
Pass by Reference, const, readonly, struct
Java Programming Language
Conditional Statements
Classes & Objects: Examples
The Building Blocks Classes: Java class library, over 1,800 classes:
Implementing Non-Static Features
Java – Inheritance.
Today’s topics UML Diagramming review of terms
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Java Inheritance.
Fundaments of Game Design
Generics, Lambdas, Reflections
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
CIS 199 Final Review.
Classes, Objects and Methods
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Advanced Programming in Java
Computer Science II for Majors
Presentation transcript:

Methods Attributes Method Modifiers ‘static’ ‘new’, ‘virtual’, ‘override’ ‘extern’ Defined externally, using a language other than C#

Hiding and Overriding The main difference between hiding and overriding relates to the choice of which method to call where the declared class of a variable is different to the run-time class of the object it references

Method Overriding public virtual double getArea() {      return length * width; } public override double getArea()      return length * length; For one method to override another, the overridden method must not be static, and it must be declared as either 'virtual', 'abstract' or 'override'

Method Hiding class Rectangle { public double getArea()      return length * width; } class Square public new double getArea()      return length * length; Where one method 'hides' another, the hidden method does not need to be declared with any special keyword. Instead, the hiding method just declares itself as 'new'

Method Hiding A 'new' method only hides a super-class method with a scope defined by its access modifier Method calls do not always 'slide through' in the way that they do with virtual methods. So, if we declare two variables: Square sq = new Square(4); Rectangle r = sq; then double area = r.getArea(); will invoke the getArea() defined in the Rectangle class, not the Square class

Method Parameters public void foo(int x, ref int y) In C#, as in C++, a method can only have one return value You overcome this in C++ by passing pointers or references as parameters With value types, however, this does not work If you want to pass the value type by reference, you mark the value type parameter with the ‘ref’ keyword public void foo(int x, ref int y) Note that you need to use the ref keyword in both the method declaration and the actual call to the method someObject.foo(a, ref b);

Out Parameters int a, b; b = 0; someObject.foo(a, ref b); // not allowed // a has not been initialized a = 0; b =0; someObject.foo(a,ref b); // now it is OK

Out Parameters C# provides the ‘out’ keyword which indicates that you may pass in un-initialized variables and they will be passed by reference public void foo(int x, ref int y, out int z) a = 0; b = 0; someObject.foo(a, ref b, out c); // no need to initialize ‘c’

Readonly Fields Instance fields that cannot be assigned to class Pair { public Pair(int x, int y) this.x = x; this.y = y; } public void Reset() x = 0; // compile time errors y = 0; private readonly int x, y; // this declares Pair as an immutable object

The ‘is’ Operator Supports run time type information Used to test if the operator is of a certain type expression is type Evaluates to a boolean result Can be used as conditional expression It will return true if The expression is not NULL and The expression can be safely cast to type

class Dog { … } class Cat { // object o; if (o is Dog) Console.Writeline(“it’s a dog”); else if (o is Cat) Console.Writeline(“it’s a cat”); else Console.Writeline(“what is it?”);

The ‘as’ Operator It attempts to cast a given operand to the requested type The normal cast operation – (T) e – generates an InvalidCastException where there is no valid cast expression as type The ‘as’ operator does not throw an exception; instead the result returned is null expression is type ? (type) expression : (type) null

Boxing Converting any value type to corresponding object type and convert the resultant 'boxed' type back again int i = 123; object o = i; // value of i is copied to the object ‘o’ if (box is int) // runtime type of box is returned as // boxed value type {Console.Write("Box contains an int");} // this line is printed

Interfaces Interfaces contain method signatures No access modifier, implicitly public No fields, not even static ones A class can implement many interfaces No implements keyword Notation is positional Base class first, then base interfaces class X: CA, IA, IB { … }

Delegates Similar to function pointers C/C++ function pointers lack instance based knowledge Delegate can be thought of a call-back mechanism: Please invoke this method for me when the time is right Event based

Delegates Delegates are reference types which allow indirect calls to methods A delegate instance holds references to some number of methods, and by invoking the delegate one causes all of these methods to be called The usefulness of delegates lies in the fact that the functions which invoke them are blind to the underlying methods they thereby cause to run

Delegates public delegate void Print (String s) ; … public void realMethod (String myString) {     // method code } Another method in the class could then instantiate the 'Print' delegate in the following way, so that it holds a reference to 'realMethod': Print delegateVariable = new Print( realMethod );