And other languages….  Get out a piece of paper  You’ll be tracing some code (quick exercises) as we go along.

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.
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 211 Inheritance AAA.
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.
Inheritance Inheritance Reserved word protected Reserved word super
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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Computer Science I Inheritance Professor Evan Korth New York University.
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.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Also “open class” and other languages….  A module is a named group of methods, constants, and class variables  All classes are modules  Modules are.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Object-Oriented Programming Chapter Chapter
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Also “open class” and other languages…
Ruby Inheritance and other languages….
Inheritance.
Lecture 12 Inheritance.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
Variables All variables hold object references
Final and Abstract Classes
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
CS240: Advanced Programming Concepts
CSC 143 Inheritance.
Java Programming Language
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
Advanced Java Programming
Java Inheritance.
Ruby Classes.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

and other languages…

 Get out a piece of paper  You’ll be tracing some code (quick exercises) as we go along

 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

 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

 We know: Every Ruby object has a set of instance variables These are not defined by the class! Instance variables are created when a value is assigned  But instance variables may be created by other methods! (Since methods are inherited, may still appear to inherit variables… but may not!)  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.

class Person def = name puts "initializing" end class Student < Person def to_s puts "Name: end s = Student.new("Cyndi") puts s  is not inherited  BUT, initialize is called, 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

class Person def = name puts "initializing" end def = end def lets () puts " ing end class Student < Person def to_s puts "Name: end End p = Person.new("Peter") s.edu") s = Student.new("Cyndi") p.lets s.lets But we can effectively treat them as if they are – by calling the methods TRACE

 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!  Caution: class methods can be overridden, so it’s best to invoke class method with name of class that defines it. Compare to Java… what is initialize? Is it inherited?

class Person def = name end def greeting puts "Hi, my name is end class Student < Person def greeting puts "Hi, I'm a student and my name is end me = Person.new("Cyndi") me.greeting you = Student.new("Suzie") you.greeting TRACE

 public. methods are public by default. initialize is implicitly private (called by new)  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++

class X # public methods def fn #stuff end protected :fn def helper #stuff end private :helper end  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++

class Person def = name puts "initializing" end def = end def lets () puts " ing end private :lets end p = Person.new("Peter") p.lets  Can also put private keyword before function definitions. Will apply to multiple functions.

class AbstractGreeter def greet puts "#{greeting} #{who}" end def say_hi puts "hi" end class WorldGreeter < AbstractGreeter def greeting; "Hello"; end def who; "World"; end end WorldGreeter.new.greet # AbstractGreeter.new.greet (error!) AbstractGreeter.new.say_hi concrete class: defines all abstract methods of ancesters abstract methods TRACE

class Person def = name end def long_greeting puts "Hi, my name is end class Student < Person def initialize(name, major) super(name) # could do just = major end def long_greeting super puts "I am studying end me = Person.new("Cyndi") me.long_greeting you = Student.new("Suzie", "CS") you.long_greeting super is a little different from Java – how? TRACE

 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 Person def = name = 12 end def show puts "Person: end class Student < Person def make_something = 15 end def show puts "Student: and end 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

 540/difference-between-class-variables- and-class-instance-variables 540/difference-between-class-variables- and-class-instance-variables  rstanding-class-instance-variables-in- ruby rstanding-class-instance-variables-in- ruby Explore on your own