Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Objective-C Lecture 2 Memory management Memory management in OC is semi- automatic: The programmer must allocate memory for objects either a) explicitly.
Black Jack in Objective-C
Object Oriented Programming with Java
A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Lecture 8. MIPS Instructions #4 – Branch Instructions #2
Written by: Dr. JJ Shepherd
Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.
CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
IPhone Development Crash Course By Dylan Harris
Principles of Object-Oriented Software Development The language Java.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Abstract Classes and Interfaces
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.
Passing data between storyboard views Singleton pattern.
Objective-C Memory management Memory management is semi-automatic: The programmer must allocate memory for objects either a) explicitly (alloc) or b) indirectly.
Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance and Access Control CS 162 (Summer 2009)
Constructors & Garbage Collection Ch. 9 – Head First Java.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
OBJECTIVE C Kurt Ladendorf. General Knowledge  Super set of C  Smalltalk  Mainly used for iOS and other Apple development.
Classes, Interfaces and Packages
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
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.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
The Object-Oriented Thought Process Chapter 03
Polymorphism in Methods
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Variables and Their scope
Chapter 11 Inheritance and Polymorphism
Chapter 10: Method Overriding and method Overloading
Dynamic Memory.
Method Overriding and method Overloading
Object-Oriented Programming (OOP) Lecture No. 44
Presentation transcript:

Lecture 3 - Writing Initializers A correct initializer must comply with the following: The initializer name should begin with init The initializer returns the initialized object (self) The initializer calls the initializer of the superclass (this will propagate up the chain of inheritance until finally the init() of NSObject is called) The initializer uses the value returned by the superclass init() The initializer must consider and correctly handle construction errors.

Method syntax Definition is preceded by + or – and the return type as a C- cast: -(float) result If a method has no parameters, definition and call consist only of the method name Each parameter is represented by a :, followed by the parameter type as a C-cast, followed by the parameter name method1 :(int)par1 Before each colon one can put a label, the labels become part of the method name, compare the two definitions below: -(float)sum:a :b :c -(float)sumofA:a andB:b andC:c

example continued Definition Example of call (float)sum:a :b :c [ob1 sum :1.1 :2.2 :3.3]; { return a+b+c; } -(float)sumofA:a andB:b andC:c [ob1 sumofA:1.1 andB:2.2 andC:3.3]; { return a+b+c: } The two methods do the same but the second reads much easier. Read the detailed explanations in the lecture notes.

Initializers continued Initializer with arguments, example from class HelloWorld -(id)initwithNum:n andValue:v { num = n; value = v; return self; } The big difference is the return self at the end, and the return type of id, this is called dynamic typing. id is the same as void* in C. Now we can write: id hw = [[HelloWorld alloc] initWithNum:3 andValue:3.7]; This initializer is much better but still not totally correct.

Initializers continued The call to alloc returns a new object. To this object we should send init by sending the inherited init to the parent class. The initializer should continue only if this init succeeds: -(id)initwithNum:n andValue:v { if (!(self = [super init])) return nil; self->num = n; self->value = v; return self; } The whole call becomes more complicated, but is explained in the lecture.

A new Student : NSObject { NSString* name; float gpa; } -(id)init; -(id)initStudent: (NSString*)name

class Student -(id)init // default initializer { if (!(self = [super init])) return nil; self->name = nil; self->gpa = 0; return self; } -(id)initStudent: (NSString*)name :(float)gpa { if (!(self = [super init])) return nil; self->name = name; self->gpa = gpa; return self;