The Object-Oriented Thought Process Chapter 03

Slides:



Advertisements
Similar presentations
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Advertisements

1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Programming Languages and Paradigms Object-Oriented Programming.
Object Oriented Programming
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Object Oriented Programming Elhanan Borenstein Lecture #4.
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.
Java Implementation: Part 3 Software Construction Lecture 8.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ECE122 Feb. 22, Any question on Vehicle sample code?
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Chapter 5 Introduction to Defining Classes
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
OOP Basics Classes & Methods (c) IDMS/SQL News
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Design issues for Object-Oriented Languages
Written by: Dr. JJ Shepherd
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Chapter 20 Generic Classes and Methods
University of Central Florida COP 3330 Object Oriented Programming
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Types of Programming Languages
Understanding Inheritance
Object Based Programming
The Object-Oriented Thought Process Chapter 05
Chapter 6 Methods: A Deeper Look
Java Programming Language
Encapsulation and Constructors
Advanced Java Programming
Object Oriented Programming in java
Java – Inheritance.
Java Inheritance.
Chapter 12: Exceptions and Advanced File I/O
Java Programming Language
CMSC 202 Exceptions.
Creating and Using Classes
SPL – PS3 C++ Classes.
Presentation transcript:

The Object-Oriented Thought Process Chapter 03 Advanced Object-Oriented Concepts

Constructors Constructors are used to initialize objects. In Java and C#, constructors are methods that share the same name as the class. Visual Basic .NET uses the designation New. Objective-C uses the init keyword.

When a Constructor is Called When a new object is created, one of the first things that happens is that the constructor is called. The constructor creates a new instance of the class, thus allocating the required memory. Then the constructor itself is called, passing the arguments in the parameter list. Providing the opportunity to attend to the appropriate initialization.

What’s Inside a Constructor Perhaps the most important function of a constructor is to initialize the memory allocated. In short, code included inside a constructor should set the newly created object to its initial, stable, safe state.

The Default Constructor If the class provides no explicit constructor, a default constructor will be provided. It is important to understand that at least one constructor always exists, regardless of whether you write a constructor yourself. If you do not provide a constructor, the system will provide a default constructor for you.

Using Multiple Constructors In many cases, an object can be constructed in more than one way. To accommodate this situation, you need to provide more than one constructor. This is called overloading a method (overloading pertains to all methods, not just constructors). Most OO languages provide functionality for overloading a method.

Overloading Methods Overloading allows a programmer to use the same method name over and over. As long as the signature of the method is different each time. The signature consists of the method name and a parameter list

The Superclass When using inheritance, you must know how the parent class is constructed. Inside the constructor, the constructor of the class’s superclass is called. Each class attribute of the object is initialized. The rest of the code in the constructor executes.

Designing Constructors It is good practice to initialize all the attributes. In some languages, the compiler provides some sort of initialization. As always, don’t count on the compiler to initialize attributes! Constructors are used to ensure that the application is in a stable (or safe) state.

Error Handling Assuming that your code has the capability to detect and trap an error condition, you can handle the error in several ways: Ignore the problem—not a good idea! Check for potential problems and abort the program when you find a problem. Check for potential problems, catch the mistake, and attempt to fix the problem. Throw an exception. (Often this is the preferred way to handle the situation.)

The Concept of Scope Multiple objects can be instantiated from a single class. Each of these objects has a unique identity and state. Each object is constructed separately and is allocated its own separate memory.

Types of Scope Methods represent the behaviors of an object; the state of the object is represented by attributes. Local attributes Object attributes Class attributes

Local Attributes Local attributes are owned by a specific method Local variables are accessible only inside a specific method. In Java, C#, C++ and Objective-C, scope is delineated by curly braces ({ }). public method() { int count; }

Object Attributes In many design situations, an attribute must be shared by several methods within the same object. public class Number { int count; // available to both method1 and method2 public method1() { count = 1; } public method2() { count = 2;

Class Attributes It is possible for two or more objects to share attributes. In Java, C#, C++ and Objective-C, you do this by making the attribute static: public class Number { static int count; public method1() { }

Operator Overloading Some OO languages allow you to overload an operator. C++ is an example of one such language. Operator overloading allows you to change the meaning of an operator. More recent OO languages like Java, .NET, and Objective-C do not allow operator overloading.

Multiple Inheritance Multiple inheritance allows a class to inherit from more than one class. Multiple inheritance can significantly increase the complexity of a system, Java, .NET, and Objective-C do not support multiple inheritance (C++ does). In some ways interfaces compensates for this.

Object Operations Comparing primitive data types is quite straightforward. Copying and comparing objects is not quite as simple. The problem with complex data structures and objects is that they might contain references. Simply making a copy of the reference does not copy the data structures or the object that it references.