OOP with Objective-C Categories, Protocols and Declared Properties.

Slides:



Advertisements
Similar presentations
Java Review Interface, Casting, Generics, Iterator.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Guide To UNIX Using Linux Third Edition
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Data Structures Using C++ 2E
Programming Languages and Paradigms Object-Oriented Programming.
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Introduction to Object-Oriented Programming
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.
Objective C Basics. It’s C with Some Extras!  Cross Platform language  Mac  Linux/UNIX  Windows  Layer above C (Superset)  Adds Object-Oriented.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
CSC 142 Computer Science II Zhen Jiang West Chester University
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Agenda Object Oriented Programming Reading: Chapter 14.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
View Controllers Content taken from book: “iPhone SDK Development” by Bill Dudney and Chris Adamson.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
RUBY by Ryan Chase.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
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.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Classes, Interfaces and Packages
Written by: Dr. JJ Shepherd
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Inheritance and Polymorphism
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
OOP Basics Classes & Methods (c) IDMS/SQL News
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Lecture 5:Interfaces and Abstract Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Objects as a programming concept
Objects as a programming concept
Programming Language Concepts (CIS 635)
Java Programming Language
CSC 113: Computer programming II
Polymorphism Polymorphism
Interfaces.
Chapter 5 Classes.
Presentation transcript:

OOP with Objective-C Categories, Protocols and Declared Properties

What is a Category? Objective-C categories provide a means to add methods to a class. Methods that you add through a category become part of the class definition. If you add a method to the NSString class, any instance, or subclass, of NSString will have access to that method. Defining a category is identical to defining the interface for a class, with one small exception: you add a category name inside a set of parenthesis after the interface declaration. The format is shown on the next slide.

Category ClassToAddMethodsTo (category)...methods go For example, below is a category that adds a method to the NSString class. The method reverseString adds the capability to all NSString objects to reverse the characters in the NSString (reverse) -(NSString *) See the ReverseString Tutorial

What is a Protocol? Formal Protocols: A formal protocol is a collection of method declarations defined independently of any class, Prot1 -(void) print; -(void) show; -(float) Informal Protocols: These are actually categories.

Adopting a Protocol The protocol Prot1 can be adopted by any The class Student must implement the methods in the protocol Prot1 in its implementation.

Failure to implement If the class that adopts a protocol does not implement the methods in the protocol, the compiler will issue a warning for each missing method. As long as the adopted methods are not called, there will be no consequences for the running code.

Checking for conformity During running of the program, checking can be performed whether or not a given class (or a given object) conforms to a given protocol. The checking is done like below: pt is a pointer to an object, prot1 is a pointer to a Protocol [pt conformsToProtocol:prot1] This method returns true if the class of the object pt has adopted the protocol and false otherwise. It does not check whether the methods in the protocol are implemented or not.

Grouping of classes Protocols allow the grouping of classes and objects according to which protocols they have implemented. This grouping overrides hierarchical relationships.

Declared Properties Declaration: If a property is supposed to provide accessors for the instance variable float value, then declare in the interface of the float value; They provide a shortcut for defining getter and setter methods for the instance variables of a class. They serve only the convenience of the programmer – they are not an essential feature. Everything they do can be done with the basic Objective-C features alone. They work in conjunction with directive.

Declared Properties Specifics If the programmer sticks to the defaults, then a) There is convenience in programming the accessor methods b) The name of the declared property is the same as the instance variable: xyz c) The created accessor methods are: xyz for the getter, setXyz for the setter

Overriding Defaults All defaults for properties can be overridden. The name of the property can be different than the instance variable The names of the getter and setter can be set as desired The property can be set to do either assign, or retain or copy in the setter method. directive tells which instance variable a declared property refers to

Summary of Categories, Protocols and Properties Try them out. See the examples provided. Categories are sort of like Interfaces in other object-oriented programming languages. A Protocol is sort of like an interface or abstract class as well in other object-oriented programming languages.