C# F 1 CSC 298 Object Oriented Programming (Part 1)

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
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.
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.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
LECTURE 07 Programming using C# Inheritance
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.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
Programming Pillars Introduction to Object- Oriented Programming.
2009 first semister Instructor: Abdalrahman Obidat 1 Revision 3 Instructor: Abdalrahman Obidat.
What is inheritance? It is the ability to create a new class from an existing class.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 10 Inheritance and Polymorphism
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Polymorphism, Virtual Methods and Interfaces Version 1.1.
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 and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSC 143N 1 CSC 143 Object-Oriented Programming [Chapter 8]
CSCI-383 Object-Oriented Programming & Design Lecture 17.
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 Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CSC 143 Inheritance.
Instructor: Abdalrahman Obidat
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Java Programming Language
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

C# F 1 CSC 298 Object Oriented Programming (Part 1)

C# F 2 Introduction  Classes have proven very useful  They help us write programs the way we view the world: interface vs implementation  There is more to come! C# supports object oriented programming, i.e.,  Inheritance (sub classing)  Dynamic dispatch (polymorphism)  Two very powerful programming tools!  They help us write less code

C# F 3 Composition: has-a relationship  We have used many times several classes together, e.g.,  Use an instance of a class as an instance field of another class public class Landscape { private Mountain m = new Mountain(); }  This a "has-a" relationship  A Landscape "has-a" Mountain  Also called aggregation

C# F 4 Organization hierarchy  Often, we classify things according to a hierarchy (from general to specific), e.g. part of the organization of a university UniversityMember Staff Faculty Lecturer Professor Student Freshman Sophomore

C# F 5 Inheritance: is-a relationship  Objects have also an "is-a" relationship  A Freshman "is-a" Student, a Lecturer "is-a" UniversityMember  C# gives us the tools to implement an "is-a" relation. We can map our view of the world on the computer

C# F 6 Inheritance in C#  Inheritance is a way to encode the "is-a" relation in OO languages  Freshman declares that it "is-a" Student by inheriting from it  A derived class inherits from a base class by writing " : BaseClassName " in the derived class declaration public class Freshman : Student { /* Freshman-specific stuff here */} derived class (or subclass) base class (or superclass)

C# F 7 What is inherited? (1)  The subclass inherits all of the members(data+methods) of its superclass.  Members declared public or protected within the superclass can be used by the subclass as if they were declared within the subclass itself.  protected member of a class: visible within the class itself and the subclasses (even if they are in a different assembly). More on protected later.

C# F 8 What is inherited? (2)  private members of the superclass are unavailable outside the superclass scope.  assembly level (=declared internal) members of the superclass are unavailable outside the assembly scope. If the superclass and subclass are in the same assembly, the subclass can access any assembly level members of the superclass  A subclass instance is also a superclass instance (is-a relation).  All methods of the superclass can be used on a subclass instance.

C# F 9 Scope example  Base.cs public class Base { public int i; //visible everywhere internal int j;//visible within the assembly protected int k; //visible within //any derived class private int l;// visible within Base only }  Derived.cs public class Derived: Base{ public void update(){ i=4; // OK j=5; // OK if in the same assembly // Error if not in the same assembly k=6; // OK l=7; /*Error (not in Base)*/}}

C# F 10 Example: Person, Student  A person has a name, an age and can speak  A student is a person with a gpa Person name: String age: int speak: void Student gpa: double derived from

C# F 11 Person class public class Person{ public String name; // any name is OK private int age; // 0<=age<=130 //Call the other constructor public Person():this("someone",20) {} public Person(String s, int a) { name = s; Age = a; } public int Age{ // Age property set {if (value>=0 && value<=130) age=value;} get {return age;} } public void speak() { Console.WriteLine("Hi, I am "+name);} }

C# F 12 Student class public class Student: Person{ private double gpa; //between 0 and 4.0 public Student()//Person() automatically called { GPA = 3.5; } public Student(String s,int a,double g) :base(s,a)//Call the constructor Person(s,a) { GPA = g; } public double GPA { // GPA property set{ if (value>=0 && value<=4.0) gpa=value;} get{ return gpa;} } }

C# F 13 Using Person and Student Person p = new Person("Jane",25); Student s = new Student("Robert",28,3.8); p.speak(); // a person can speak s.speak(); // a student can speak, since // a student is a person

C# F 14 Method overriding  How can we specialize speak for the Student class?  Define speak as virtual in the Person class public virtual void speak(){ /*code*/ }  Redefine speak within the Student class public override void speak(){ base.speak();//let the Person speak // Add what the student say Console.WriteLine("My gpa is "+gpa); }  For any Student instance s, s.speak() invokes the speak method of the Student class. This is called method overriding.

C# F 15 this keyword revisited  this: two uses  a reference to the current object when within one of the object instance methods  Use this(argument list) when calling another constructor from a constructor (within the same class). The call is the first executed statement in the constructor. public Person(): this(name,20) { /* more code here if necessary */ }

C# F 16 base keyword  base: two uses  a reference to the base object part of the current derived object within one of the derived object instance method. base.speak();//In Student.speak()  Use base(argument list) when calling a base constructor from a derived constructor. The call is the first executed statement in the constructor. public Student(string s, int a, double g): base(s,a) { gpa = g; }  Can't chain the base calls base.base.method();//Error

C# F 17 Constructor calls  When calling a constructor, C#  invokes the base class constructor (if necessary via an implicit call to base()),  then, initializes the instance variables of the class,  then, executes the statements in the constructor. public class Derived: Base{ private int i=3; public Derived(){i++;}} // And in some method in some other class Derived d = new Derived(); // call Base(), initialize any instance field // in class Base, execute Base(), // set i to 3 in class Derived, // increment i as instructed in Derived() // What happens if Base is derived from // some other class?

C# F 18 Overloading and Overriding  Method overloading: same name but a different number or type of arguments.  A subclass can define some overloaded methods that augment the inherited overloaded methods provided by a base class.  Method overriding: define a method in the subclass with the same signature (return type and arguments) as the inherited method.  The method in the subclass shadows the method of the superclass.  Powerful when dealing with a hierarchy of objects (polymorphism: next slide)

C# F 19 Polymorphism example  Consider Person[] group=new Person[2]; group[0]=new Person("Jane",25); group[1]=new Student("Bob",26,3.9); //OK, a Student is a Person for(int i=0; i<2; i++) group[i].speak(); // What is printed?  Note: Properties are just syntactic sugar for get and set methods. Overriding works the same way for instance properties as for instance methods.

C# F 20 Polymorphism  If there are several implementations of a virtual method in the inheritance hierarchy of an object, the one in the most derived class always overrides the others.  This is the case even if we refer to the object by way of a less derived type. group[1].speak(); //invokes speak of Student //even though group is an // array of type Person[]

C# F 21 Dynamic binding and Polymorphism  Polymorphism uses dynamic binding  The selection of the method is done at run time. The CLR looks at the actual type of the object referred to.  To turn on overriding, the C# programmer must mark the base instance method as virtual and the derived instance method as override (it is a compiler error to do one without the other).  To prevent overriding of a method, just don't use virtual. However, it is possible to shadow the method in a derived class by marking it as new (see next slide).

C# F 22 new keyword on a method // in Person public void speak(){} //can't override since not virtual // in Student public new void speak(){} // shadows speak in Person // in some method of some other class Student s = new Student(); s.speak(); // Student.speak Person p = s; p.speak(); // Person.speak (static binding) // With overriding, Student.speak would // have been called.

C# F 23 Static binding  When the overriding feature is turned off (no virtual keywork), the compiler uses the static type of the variable to select the instance method.  Static binding is slightly more efficient than dynamic binding.

C# F 24 Static methods (1)  Reminder: static methods are not attached to any instance of a class. They belong to the class.  A static method is invoked by using the class name. They are bound at compile time. (That's why they are called static) Math.Sqrt(2);  A class inherits all of the public and protected static methods of a base class. But there is no overriding mechanism with static methods.  However, you can shadow a base class static method in the subclass (using the keyword new as for a non virtual instance method).

C# F 25 Static methods (2) public class Base{ public static void foo(){ Console.WriteLine("foo of Base");} } public class Derived: Base{ public static void foo(){// No Console.WriteLine("foo of Derived");} public new static void foo(){ // OK Console.WriteLine("foo of Derived");} }

C# F 26 sealed keyword  The class can't be inherited. public sealed class ThisIsIt{ /* class code */ } public class MoreOfIt: ThisIsIt {} // Error  None of the methods in a sealed class are overridden. The compiler can optimize the use of the class methods (e.g. by inlining short methods).