Ruby Inheritance and other languages….

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
And other languages….  Get out a piece of paper  You’ll be tracing some code (quick exercises) as we go along.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Object-Oriented PHP (1)
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Object Oriented Programming
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Design issues for Object-Oriented Languages
Also “open class” and other languages…
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Final and Abstract Classes
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
CS240: Advanced Programming Concepts
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
Java Programming Language
Advanced Java Topics Chapter 9
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
More Object-Oriented Programming
Computer Programming with JAVA
Java Inheritance.
CISC/CMPE320 - Prof. McLeod
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
Presentation transcript:

Ruby Inheritance and other languages…

Class Participation Get out a piece of paper You’ll be tracing some code (quick exercises) as we go along Also: have RubyInheritance-n.rb files ready to be opened – you can run those to verify your trace is correct

Extending Class Behavior May create subclasses (inheritance) May include/inherit methods from modules (mix-ins) Clients of class may also extend: Classes are open; any program can add a method Can add a singleton method to an individual object Modules covered next lecture

Inheritance Object is the superclass if none specified Every class has a single superclass May have multiple subclasses – a hierarchy Syntax: class Student < Person End BasicObject is parent of Object Can create completely separate hierarchy (e.g., BasicObject doesn’t include Kernel… so puts etc.) Few methods, useful for wrapper classes

Inheritance and instance variables We know: Every Ruby object has a set of instance variables Instance variables are created when a value is assigned (within a method, often initialize) Instance variables are not just defined as part of the class (i.e., don’t put @x outside any method) Therefore, instance variables have nothing to do with inheritance. BUT, if all variables are defined in initialize, inheritance appears to work as expected What are some pros and cons? What about “shadowing”? On your paper: write a few lines of code to show shadowing in Java.

Simple Example Technically @name is not inherited class Person def initialize(name) @name = name puts "initializing" end class Student < Person def to_s puts "Name: #{@name}" s = Student.new("Cyndi") puts s Technically @name is not inherited BUT, initialize is called, so @name is created for the Student object It appears that the variable is inherited An instance variable created in a parent method that is not called by the child will not exist Ruby-Inheritance-1.rb

Instance variables not inherited TRACE – what is displayed? class Student < Person def to_s puts "Name: #{@name}" end End p = Person.new("Peter") p.setupEmail("peter@mines.edu") s = Student.new("Cyndi") p.letsEmail s.letsEmail class Person def initialize(name) @name = name puts "initializing" end def setupEmail(email) @email = email def letsEmail() puts "Emailing #{@email}" But we can effectively treat them as if they are – by calling the methods Ruby-Inheritance-1a.rb

Inheriting and Overriding Can override methods Methods are bound dynamically (when executed) not statically (when parsed) Methods like to_s and initialize are automatically inherited Caution: if you don’t know all methods of a class you’re subclassing, you may override a private method accidentally!

Language Comparison run RubyInheritance-1.rb Does Java automatically call the parent ctor? http://stackoverflow.com/questions/6318628/when-do-you-need-to- explicitly-call-a-superclass-constructor Compare to C++: http://stackoverflow.com/questions/120876/c- superclass-constructor-calling-rules Class participation (add to your paper): In Java when do you need to explicitly call the parent ctor? In C++, why don’t they just use a keyword like super to all the parent ctor? Interesting info – but not exam topics

Language Comparison Assume you’re writing a C++ program which has: parent named Bug, child named Mosquito method named bite exists in parent & child What do you need to do to make sure this is bound dynamically? What happens if this is not bound dynamically? Write a few lines of C++ (just on paper) to illustrate. In case you need a reminder: http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-functions-in-c

Big Picture When dealing with an OO language Inheritance should be part of the language There is typically some way to ensure parent and child variables get initialized, but syntax varies Child classes can usually call parent class methods (otherwise what’s the point) Child classes can usually override the behavior of parent class methods – but you need to think about whether that happens at runtime (late binding) or compile time (early binding)

Override parent method TRACE – what is displayed? me = Person.new("Cyndi") me.greeting you = Student.new("Suzie") you.greeting class Person def initialize(name) @name = name end def greeting puts "Hi, my name is #{@name}" class Student < Person puts "Hi, I'm a student and my name is #{@name}" Ruby-Inheritance-2.rb

Method Visibility public. private. protected Applies only to methods! methods are public by default. initialize is implicitly private (called by new, error if you try to call directly) private. only visible to other methods of the class (or subclass) implicitly invoked on self (but can’t write self.fn) protected like private, but can be invoked on any instance of the class (e.g., if pass in parameter… allows objects of same type to share state) used infrequently Applies only to methods! Variables are automatically encapsulated (private) Constants are public Compare to Java/C++ Why does protected not have the same meaning as in C++?

Method visibility class X # public methods def fn #stuff end protected :fn def helper private :helper can override visibility, e.g., private_class_method :new private and protected guard against inadvertent use – BUT, with metaprogramming it’s possible to invoke these methods. Compare to Java/C++

Simple example p = Person.new("Peter") p.setupEmail("p@x.edu") class Person def initialize(name) @name = name puts "initializing" end def setupEmail(email) @email = email def letsEmail() puts "Emailing #{@email}" private :letsEmail p = Person.new("Peter") p.setupEmail("p@x.edu") p.letsEmail Can also put private keyword before function definitions. Will apply to multiple functions. Ruby-Inheritance-2a.rb

Abstract Class TRACE class AbstractGreeter def greet puts "#{greeting} #{who}" end def say_hi puts "hi" class WorldGreeter < AbstractGreeter def greeting; "Hello"; end def who; "World"; end WorldGreeter.new.greet # AbstractGreeter.new.greet (error!) AbstractGreeter.new.say_hi abstract methods concrete class: defines all abstract methods of ancesters On your paper: What makes AbstractGreeter an abstract class? How does this compare to Java? C++? Ruby-Inheritance-3.rb

Chaining methods TRACE me = Person.new("Cyndi") me.long_greeting you = Student.new("Suzie", "CS") you.long_greeting class Person def initialize(name) @name = name end def long_greeting puts "Hi, my name is #{@name}." class Student < Person def initialize(name, major) super(name) # could do just super @major = major super puts "I am studying #{@major}." Super syntax is a little different from Java – how? Ruby-Inheritance-4.rb

Class variables - review When did we use static (class) variables in Java/C++? We’ll use class variables in Ruby for similar purposes. But syntax is different.

Class variable example TRACE me = Person.new("Cyndi") you = Student.new("Suzie") # creates class variable something you.make_something you.show who = Student.new("Joe") # both Students can access something who.show # parent cannot access something # me.show # error class Person def initialize(name) @name = name @@what = 12 end def show puts "Person: #{@@something}" class Student < Person def make_something @@something = 15 puts "Student: #{@@something} and #{@@what}" Ruby-Inheritance-5.rb

Class Instance Variables http://stackoverflow.com/questions/3802540/difference-between-class-variables-and-class-instance-variables http://blog.codegram.com/2011/4/understanding-class-instance-variables-in-ruby Not covered in csci400, but you may want to explore on your own