Module 7: Essentials of Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Object Oriented Programming. Object Oriented Data and operations are grouped together Account Withdraw Deposit Transfer Interface: Set of available operations.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object-oriented programming and software development Lecture 1.
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Information Systems Engineering
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Learners Support Publications Object Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Object-Oriented Design
Name Spaces: ALL versus OOL
Classes and OOP.
2.7 Inheritance Types of inheritance
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Table of Contents Class Objects.
Computing with C# and the .NET Framework
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Packages, Interfaces & Exception Handling
Object Oriented Programming
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Chapter Three - Implementing Classes
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Dr. Bhargavi Dept of CS CHRIST
Inheritance Inheritance is a fundamental Object Oriented concept
CPS120: Introduction to Computer Science
By Rajanikanth B OOP Concepts By Rajanikanth B
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Module 7: Essentials of Object-Oriented Programming

Overview Classes and Objects Using Encapsulation C# and Object Orientation Defining Object-Oriented Systems

 Classes and Objects What Is a Class? What Is an Object? Comparing Classes to Structs Abstraction

What Is a Class? For the philosopher… An artifact of human class ification! Class ify based on common behavior or attributes Agree on descriptions and names of useful class es Create vocabulary; we communicate; we think! For the object-oriented programmer… A named syntactic construct that describes common behavior and attributes A data structure that includes both data and functions CAR?

What Is an Object? An object is an instance of a class Objects exhibit: Identity: Objects are distinguishable from one another Behavior: Objects can perform tasks State: Objects store information

Comparing Classes to Structs A struct is a blueprint for a value No identity, accessible state, no added behavior A class is a blueprint for an object Identity, inaccessible state, added behavior struct Time class BankAccount { public int hour;... public int minute;... } struct Time class BankAccount { public int hour;... public int minute;... }

Abstraction Abstraction is selective ignorance Decide what is important and what is not Focus and depend on what is important Ignore and do not depend on what is unimportant Use encapsulation to enforce an abstraction The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise. Edsger Dijkstra The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise. Edsger Dijkstra

 Using Encapsulation Combining Data and Methods Controlling Access Visibility Why Encapsulate? Object Data Using Static Data Using Static Methods

Combining Data and Methods Combine the data and methods in a single capsule The capsule boundary forms an inside and an outside Withdraw( ) Deposit( ) balance Withdraw( ) Deposit( ) balance BankAccount ?

Controlling Access Visibility Methods are public, accessible from the outside Data is private, accessible only from the inside Withdraw( ) Deposit( ) balance Withdraw( ) Deposit( ) balance  BankAccount ?

Why Encapsulate? Allows control Use of the object is solely through the public methods Allows change Use of the object is unaffected if the private data type changes Withdraw( ) Deposit( ) dollars 12 Withdraw( ) Deposit( ) balance cents 56 

Object Data Object data describes information for individual objects For example, each bank account has its own balance. If two accounts have the same balance, it is only a coincidence. Withdraw( ) Deposit( ) balance owner "Bert" Withdraw( ) Deposit( ) balance owner "Fred"

Using Static Data Static data describes information for all objects of a class For example, suppose all accounts share the same interest rate. Storing the interest rate in every account would be a bad idea. Why? Withdraw( ) Deposit( ) balance interest 7% Withdraw( ) Deposit( ) balance interest 7% 

Using Static Methods Static methods can only access static data A static method is called on the class, not the object InterestRate( ) interest 7% Withdraw( ) Deposit( ) balance owner "Fred" An account objectThe account class Classes contain static data and static methods Objects contain object data and object methods  

 C# and Object Orientation Hello, World Revisited Defining Simple Classes Instantiating New Objects Using the this Operator Creating Nested Classes Accessing Nested Classes

Hello, World Revisited using System; class Hello { public static int Main( ) { Console.WriteLine("Hello, World"); return 0; } using System; class Hello { public static int Main( ) { Console.WriteLine("Hello, World"); return 0; }

Defining Simple Classes Data and methods together inside a class Methods are public, data is private class BankAccount { public void Withdraw(decimal amount) {... } public void Deposit(decimal amount) {... } private decimal balance; private string name; } class BankAccount { public void Withdraw(decimal amount) {... } public void Deposit(decimal amount) {... } private decimal balance; private string name; } Public methods describe accessible behaviour Public methods describe accessible behaviour Private fields describe inaccessible state Private fields describe inaccessible state

Instantiating New Objects Declaring a class variable does not create an object Use the new operator to create an object class Program { static void Main( ) { Time now; now.hour = 11; BankAccount yours = new BankAccount( ); yours.Deposit(999999M); } class Program { static void Main( ) { Time now; now.hour = 11; BankAccount yours = new BankAccount( ); yours.Deposit(999999M); } hour minute now yours... new BankAccount object

Using the this Keyword The this keyword refers to the object used to call the method Useful when identifiers from different scopes clash class BankAccount {... public void SetName(string name) { this.name = name; } private string name; } class BankAccount {... public void SetName(string name) { this.name = name; } private string name; } If this statement were name = name; What would happen?

Creating Nested Classes Classes can be nested inside other classes class Program { static void Main( ) { Bank.Account yours = new Bank.Account( ); } class Bank {... class Account {... } } class Program { static void Main( ) { Bank.Account yours = new Bank.Account( ); } class Bank {... class Account {... } } The full name of the nested class includes the name of the outer class

Accessing Nested Classes Nested classes can also be declared as public or private class Bank { public class Account {... } private class AccountNumberGenerator {... } } class Program { static void Main( ) { Bank.Account accessible; Bank.AccountNumberGenerator inaccessible; } class Bank { public class Account {... } private class AccountNumberGenerator {... } } class Program { static void Main( ) { Bank.Account accessible; Bank.AccountNumberGenerator inaccessible; } 

Lab 7.1: Creating and Using Classes

 Defining Object-Oriented Systems Inheritance Class Hierarchies Single and Multiple Inheritance Polymorphism Abstract Base Classes Interfaces Early and Late Binding

Inheritance Inheritance specifies an “is a kind of" relationship Inheritance is a class relationship New classes specialize existing classes Musician Violin Player Violin Player Base class Derived class Generalization Specialization Is this a good example of inheritance ?

Class Hierarchies Classes related by inheritance form class hierarchies Musician ??? String Musician String Musician Violin ??? Musical Instrument Musical Instrument plays Violin Player Violin Player Stringed Instrument Stringed Instrument

Single and Multiple Inheritance Single inheritance: deriving from one base class Multiple inheritance: deriving from two or more base classes Stringed Instrument Stringed Instrument Violin Musical Instrument Musical Instrument Stringed Instrument Stringed Instrument Pluckable Violin has a single direct base class Stringed Instrument has two direct base classes

Polymorphism The method name resides in the base class The method implementations reside in the derived classes String Musician TuneYourInstrument( ) Guitar Player TuneYourInstrument( ) Violin Player TuneYourInstrument( ) A method with no implementation is called an operation A method with no implementation is called an operation

Abstract Base Classes Some classes exist solely to be derived from It makes no sense to create instances of these classes These classes are abstract Stringed Musician { abstract } Stringed Musician { abstract } Guitar Player « concrete » Guitar Player « concrete » Violin Player « concrete » Violin Player « concrete » You can create instances of concrete classes You can create instances of concrete classes You cannot create instances of abstract classes

Interfaces Interfaces contain only operations, not implementation String Musician { abstract } String Musician { abstract } Violin Player « concrete » Violin Player « concrete » Musician « interface » Musician « interface » Nothing but operations. You cannot create instances of an interface. Nothing but operations. You cannot create instances of an interface. May contain some implementation. You cannot create instances of an abstract class. Must implement all inherited operations. You can create instances of a concrete class.

Early and Late Binding Normal method calls are resolved at compile time Polymorphic method calls are resolved at run time Musician « interface » Musician « interface » Violin Player « concrete » Violin Player « concrete » Late binding Early binding runtime TuneYourInstrument( )

Review Classes and Objects Using Encapsulation C# and Object Orientation Defining Object-Oriented Systems