Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
Object Oriented Programming
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
LECTURE 07 Programming using C# Inheritance
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
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.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
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.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance and Access Control CS 162 (Summer 2009)
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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)
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Advanced Programming in Java
OOP: Encapsulation &Abstraction
Advanced Programming in Java
Lecture 12 Inheritance.
Object-Oriented Programming: Inheritance
Objects as a programming concept
Inheritance and Polymorphism
Advanced Programming in Java
Road Map Inheritance Class hierarchy Overriding methods Constructors
Lecture 14 Writing Classes part 2 Richard Gesick.
Lecture 13 Writing Classes Richard Gesick.
Overloading and Constructors
Lecture 22 Inheritance Richard Gesick.
Inherited Classes in Java
Advanced Programming Behnam Hatami Fall 2017.
CIS 199 Final Review.
Object-Oriented Design AND CLASS PROPERTIES
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Advanced Programming in Java
Object-Oriented Design AND CLASS PROPERTIES
CS 240 – Advanced Programming Concepts
Presentation transcript:

Object-Oriented Programming

Agenda Static Const Overloading Inheritance

Static Each instance of a class (called an object) has a copy of the attributes Changing an attribute in one object doesn’t affect the attribute of another object But what if we want persistence (shared) among all instances?

Static Variables void PrintNumbers () { static int count = 0; // this is only set to 0 ONCE Console.WriteLine(count); count++; } static void Main() ... for(i=0; i < 50; i++) PrintNumbers();

Static Attributes class BMW_Z4 { private static int VehicleId = 0; public int MyID; public BMW_Z4 () MyID = VehicleId; VehicleId++; } ... static void Main() BMW_Z4 my_z4 = new BMW_Z4(); // has VehicleId of 0 BMW_Z4 your_z4 = new BMW_Z4(); // has VehicleId of 1

Const Variables are just that – variable They can be changed programmatically via the assignment operator (=) But there are times when some values should be immutable Preface the declaration with “const” Cannot be changed – EVER!

Const Example class BMW_Z4 { public const int MaxSpeed = 185; private int currentSpeed; public void Accelerate currentSpeed += 5; if (currentSpeed > MaxSpeed) currentSpeed = MaxSpeed; } ...

Overloading Overloading involves using the same method/function name Vary the number of parameters Vary the type of parameters Cannot just change return type (ambiguity in invocation) Useful to keep things simple Squaring a number…

Overloading Example int Square (int i) { return i * i; } float Square (float i) static void Main() ... float x = Square(5.3); int y = Square(9);

Operator Overloading Notice same method name, different parameters class BMW_Z4 { ... public BMW_Z4 () Initialize(0, 2004, false); } public BMW_Z4 (int my) Initialize(0, my, false); private Initialize(int cs, int my, bool tu) currentSpeed = cs; ModelYear = my; TopUp = tu; Notice same method name, different parameters Place common initialization in a separate function

Inheritance Inheritance allows one class to take on the properties of another Superclass-subclass relationship Sometimes called parent-child relationship Use the keyword extends to express this relationship Subclass will “inherit” certain attributes and methods Benefit: good design, reuse of code

Class Hierarchy Mammal LandMammal Dog Chihuahua SheepDog int weight giveBirth( ) LandMammal int numLegs Question: how many attributes does Dog have? Dog boolean rabid Chihuahua SheepDog

Things to Note LandMammal is a superclass to Dog, but a subclass to Mammal Dog has three attributes weight, numLegs and rabid Two from inheritance, one it declared itself

Visibility and Inheritance Public – allows all to see Private – allows only class in which defined to see Protected – allows class and all subclasses that inherit to see Consequently, we’ll now use protected instead of private by default… (common)

C# Syntax Notice the use of “:” class Person { ... } class Student : Person class Professor : Person Notice the use of “:”

The Base Class: “Object” All classes in C# inherit (sometimes implicitly) from Object Includes common set: ToString() GetType() Equals() Often useful to override (implement) these virtual functions “public override string ToString()…”

FIN