More on Classes Inheritance Copyright © 2012 by Yong-Gu Lee

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Programming in Objective-C Classes, Objects, and Methods Data Types and Expressions Copyright © 2012 by Yong-Gu Lee
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.
Inheritance Inheritance Reserved word protected Reserved word super
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Java Class and Inheritance.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Encapsulation by Subprograms and Type Definitions
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Intro to OOP with Java, C. Thomas Wu
C++ Classes CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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
Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Objective C. Основан на C Объектно-ориентированный Использует сообщения Динамический Протоколы Интроспекция.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
OOP with Objective-C Categories, Protocols and Declared Properties.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Programming Fundamentals Enumerations and Functions.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Creating Your Own Classes
OOP: Encapsulation &Abstraction
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
Lecture 12 Inheritance.
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Introduction to Classes
Java Programming Language
Chapter 14 Abstract Classes and Interfaces
Object-Oriented Programming
Final and Abstract Classes
Presentation transcript:

More on Classes Inheritance Copyright © 2012 by Yong-Gu Lee

More on classes

Managing classes on separate files Two files for each class (one for the interface and one for the implementation) Can be too granular

Main file // // main.m // FractionTest // // Created by 용구 이 on // Copyright (c) 2012 년 All rights reserved. // #import #import "Fraction.h" int main (int argc, const char * argv[]) { Fraction *myFraction = [[Fraction alloc] init]; // set fraction to 1/3 [myFraction setNumerator: 1]; [myFraction setDenominator: 3]; // display the fraction NSLog value of myFraction is:"); [myFraction print]; } return 0; }

Menu for new class

Class name

Fraction.h

Fraction.m

Synthesized Accessor Methods You can have your setter and getter methods (collectively known as accessor methods) automatically generated for The first step is to use directive in your interface section to identify your properties. These properties are often your instance variables. In the case of our Fraction class, the two instance variables numerator and denominator fall into this category.

@property // // Fraction.h // FractionTest // // Created by 용구 이 on // Copyright (c) 2012 년 All rights reserved. // Fraction : int numerator, denominator; -(void) print; -(double)

@synthesize // // Fraction.m // FractionTest // // Created by 용구 이 on // Copyright (c) 2012 년 All rights reserved. // #import numerator, denominator; -(void) print { NSLog numerator, denominator); } -(double) convertToNum { if (denominator != 0) return (double) numerator / denominator; else return NAN;

Accessing Properties Using the Dot Operator [myFraction numerator] myFraction.numerator The general format here is: instance.property You can use a similar syntax to assign values as well: instance.property = value This is equivalent to writing the following expression:[instance setProperty: value] Previously, you set the numerator and denominator of your fraction to 1/3 using the following two lines of code: [myFraction setNumerator: 1]; [myFraction setDenominator: 3]; Here’s an equivalent way to write the same two lines: myFraction.numerator = 1; myFraction.denominator = 3;

Multiple Arguments to Methods A method to set both the numerator and the denominator could be named and used like this: [myFraction setNumerator: 1 andDenominator: 3]; [myFraction setTo: 1 over: 3];

Example // // Fraction.h // FractionTest // // Created by 용구 이 on // Copyright (c) 2012 년 All rights reserved. // Fraction : int numerator, denominator; -(void) print; -(double) convertToNum; -(void) setTo: (int) n over: (int)

// // Fraction.m // FractionTest // // Created by 용구 이 on // Copyright (c) 2012 년 All rights reserved. // #import numerator, denominator; -(void) print { NSLog numerator, denominator); } -(double) convertToNum { if (denominator != 0) return (double) numerator / denominator; else return NAN; } -(void) setTo: (int) n over: (int) d { numerator = n; denominator = d;

// // main.m // FractionTest // // Created by 용구 이 on // Copyright (c) 2012 년 All rights reserved. // #import #import "Fraction.h" int main (int argc, const char * argv[]) { Fraction *myFraction = [[Fraction alloc] init]; [myFraction setTo: 100 over: 200]; [myFraction print]; [myFraction setTo: 1 over: 3]; [myFraction print]; } return 0; }

Methods Without Argument Names When creating the name for a method, the argument names are actually optional. For example, you can declare a method like this: -(int) set: (int) n: (int) d; Note that, unlike in previous examples, no name is given for the second argument to the method To invoke the set:: method, you use the colons as argument delimiters, as shown here: [aFraction set: 1 : 3]; -(void) setTo: (int) n over: (int) d

Local variables Variables defined within a method is local and destroyed upon exit from the method -(void)reduce { int u = numerator; int v = denominator; int temp; while (v != 0) { temp = u % v; u = v; v = temp; } numerator /= u; denominator /= u; }

The static Keyword static int hitCount = 0; -(int) showPage { static int pageCount = 0;... ++pageCount;... return pageCount; }

The local static variable would be set to 0 only once when the program started and would retain its value through successive invocations of the showPage method. Note the difference between making pageCount a local static variable and making it an instance variable. In the former case, pageCount could count the number of pages printed by all objects that invoked the showPage method. In the latter case, the variable would count the number of pages printed by each individual object because each object would have its own copy of pageCount.

The self keyword -(void) print { [self reduce]; NSLog numerator, denominator); }

Inheritance

It all begins at the Fraction: The class that has no parent is at the top of the hierarchy and is known as a root class. Parent class/Superclass Child class/Subclass

Example #import // Simple example to illustrate inheritance #import // ClassA declaration and ClassA: NSObject { int x; } ClassA -(void) initVar { x = 100; // Class B declaration and ClassB : ClassA ClassB -(void) printVar { NSLog = %i", x); int main (int argc, const char * argv[]) { ClassB *b = [[ClassB alloc] init]; [b initVar]; // will use inherited method [b printVar]; // reveal value of x; } return 0; } public

Rectangle Rectangle: int width, height; -(int) area; width, height; -(void) setWidth: (int) w andHeight: (int) h { width = w; height = h; } -(int) area { return width * height; } -(int) perimeter { return (width + height) * 2;

int main (int argc, char *argv[]) { Rectangle *myRect = [[Rectangle alloc] init]; [myRect setWidth: 5 andHeight: 8]; NSLog w = %i, h = %i", myRect.width, myRect.height); NSLog = %i, Perimeter = %i", [myRect area],[myRect perimeter]); } return 0; } [Switching to process 2263 thread 0x0] :33: porg5[2263:707] Rectangle: w = 5, h = :33: porg5[2263:707] Area = 40, Perimeter = 26

Square (inherits Square: Rectangle -(void) setSide: (int) s; Square: Rectangle -(void) setSide: (int) s { [self setWidth: s andHeight: s]; } -(int) side { return width; int main (int argc, char *argv[]) { Square *mySquare = [[Square alloc] init]; [mySquare setSide: 5]; NSLog s = %i", [mySquare side]); NSLog = %i, Perimeter = %i", [mySquare area], [mySquare perimeter]); } return 0; } :38: porg5[2329:707] Square s = :38: porg5[2329:707] Area = 25, Perimeter = 20

A Point Class and Memory Allocation Rectangle: int width, height; -(int) area; -(int) perimeter; -(void) setOrigin: (XYPoint *) pt; -(XYPoint *)

@interface XYPoint: NSObject { int x; int y; int x, y; -(void) setX: (int) xVal andY: x, y; -(void) setX: (int) xVal andY: (int) yVal { x = xVal; y = Rectangle { XYPoint *origin; } -(void) setOrigin: (XYPoint *) pt { origin = pt; } -(XYPoint *) origin { return origin;

Main int main (int argc, char *argv[]) { Rectangle *myRect = [[Rectangle alloc] init]; XYPoint *myPoint = [[XYPoint alloc] init]; [myPoint setX: 100 andY: 200]; [myRect setWidth: 5 andHeight: 8]; myRect.origin = myPoint; NSLog w = %i, h = %i", myRect.width, myRect.height); NSLog at (%i, %i)",myRect.origin.x, myRect.origin.y); NSLog = %i, Perimeter = %i", [myRect area], [myRect perimeter]); } return 0; } :43: porg5[2743:707] Rectangle w = 5, h = :43: porg5[2743:707] Origin at (100, 200) :43: porg5[2743:707] Area = 40, Perimeter = :43: porg5[2743:707] Square s = :43: porg5[2743:707] Area = 25, Perimeter = 20 Program ended with exit code: 0

Classes not owning their objects int main (int argc, char *argv[]) { Rectangle *myRect = [[Rectangle alloc] init]; XYPoint *myPoint = [[XYPoint alloc] init]; [myPoint setX: 100 andY: 200]; [myRect setWidth: 5 andHeight: 8]; myRect.origin = myPoint; NSLog at (%i, %i)", myRect.origin.x, myRect.origin.y); [myPoint setX: 50 andY: 50]; NSLog at (%i, %i)", myRect.origin.x, myRect.origin.y); } return 0; } :46: porg5[2796:707] Origin at (100, 200) :46: porg5[2796:707] Origin at (50, 50)

Classes owning their objects -(void) setOrigin: (XYPoint *) pt { if (! origin) origin = [[XYPoint alloc] init]; origin.x = pt.x; origin.y = pt.y; } The change to the setOrigin: method means that each Rectangle instance now owns its XYPoint instance. Even though it is now responsible for allocating the memory for that XYPoint, it should also now become responsible for releasing that memory. In general, when a class contains other objects, at times you will want to have it own some or all of those objects.

Overriding Methods You can’t remove or subtract methods through inheritance. However, you can change the definition of an inherited method by overriding it. A method defined with the same name as that of a parent class replaces, or overrides, the inherited definition. Your new method must have the same return type and take the same number and type of arguments as the method you are overriding.

-(void) printVar { // added method NSLog = %i", x); //////////////////////////// int main (int argc, const char * argv[]) { ClassB *b = [[ClassB alloc] init]; [b initVar]; // uses overriding method in B [b printVar]; // reveal value of x; } return 0; } // ClassA declaration and ClassA: NSObject { int x; } -(void) ClassA -(void) initVar { x = 100; // ClassB declaration and ClassB: ClassA -(void) initVar; -(void) ClassB -(void) initVar { x = 200; }

Abstract classes Sometimes classes are created just to make it easier for someone to create a subclass. For that reason, these classes are called abstract classes or, equivalently, abstract superclasses. Methods and instance variables are defined in the class, but no one is expected to actually create an instance from that class. For example, consider the root object NSObject. Can you think of any use for defining an object from that class? The abstract superclass gives a common interface for working with all types of subclassses.