Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
More about classes and objects Classes in Visual Basic.NET.
CSE341: Programming Languages Lecture 20 Blocks & Procs; Inheritance & Overriding Dan Grossman Fall 2011.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
instance variables property procedures for the mintQuantity instance variable.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Also “open class” and other languages….  A module is a named group of methods, constants, and class variables  All classes are modules  Modules are.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
+ Ruby and other programming Languages Ronald L. Ramos.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
RUBY by Ryan Chase.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
Classes, Interfaces and Packages
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
Ruby Duck Typing, Classes & Inheritance CSE 413 Autumn 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
OOP Basics Classes & Methods (c) IDMS/SQL News
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Variables All variables hold object references
PHP Classes and Objects
Week 4 Object-Oriented Programming (1): Inheritance
CIT 383: Administrative Scripting
Substitution in string value
MSIS 670 Object-Oriented Software Engineering
Interfaces.
Week 3 Object-based Programming: Classes and Objects
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
Inheritance Inheritance is a fundamental Object Oriented concept
CIT 383: Administrative Scripting
Tonga Institute of Higher Education
Ruby Classes.
Chapter 14 Abstract Classes and Interfaces
CSG2H3 Object Oriented Programming
Presentation transcript:

Ruby (on Rails) CSE 190M, Spring 2009 Week 4

Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state (constructor) – initialize() – Example: class Point def initialize(x, = x# the convention for instance = y# end

Instantiating New Objects We instantiate a new object by calling the new() method on the class we want to instantiate Example p = Point.new(2,3 How do we get of p? p.x?

Accessing State Instance variables are private by default The instance variables for our Point To access them, we must write methods that return their value – Remember "encapsulation" from CSE 142/143

Accessing State class Point def initialize(x, = = y end def end p = Point.new(2, 3) puts p.get_x # get value of x by calling a method

Accessing State class Point def initialize(x, = = y end def end p = Point.new(2, 3) puts p.x # get value of instance variable by calling a method

Accessing State We do not need to write these methods by hand Example: class Point attr_reader :x, :y def initialize(x, = = y end What if we want to assign values?

Accessing State To assign a value we can write a method Example: def = x end p.set_x(7) Similarly we can use attr_writer attr_writer :x, :y

Accessing State If we want to read and write all of our instance variables, we can combine attr_reader and attr_writer to simplify our class, replacing them with attr_accessor class Point attr_accessor :x, :y def initialize(x, = = y end

Objects in erb Objects work as expected in erb We can include the class directly in the erb file within the code tags We can also save an external.rb file (Point.rb) and then require the class file in our.erb file (plot_points.erb) require 'Point.rb' p = Point.new(3,5) The files should be in the same folder, or specify the path to the class file

Inheritance Ruby supports single inheritance This is similar to Java where one class can inherit the state and behavior of exactly one other class The parent class is known as the superclass, the child class is known as the subclass

Inheritance

Public and Private Methods Methods are public by default Private methods are declared the same way as public methods (no keyword at the beginning of method like Java) Private methods are designated by an "area" of private methods They keyword "private" designates this area Any methods after "private" are private methods

Public and Private Methods Public – any class can use the methods Private – only this particular object can use these methods There is a middle ground… methods can be "protected" Protected – only objects of this class or its subclasses can use these methods

Modifying Class Behavior Ruby allows us to add or modify functionality to ANY class This includes built-in classes like Fixnum and String Lets allow Strings to add any object to it without having to say to_s "hello" + 3 # instead of "hello" + 3.to_s