More about Classes and Objects. Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs.

Slides:



Advertisements
Similar presentations
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Advertisements

24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
BNF. What is BNF? BNF stands for “Backus-Naur Form,” after the people who invented it BNF is a metalanguage--a language used to describe another language.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Inheritance Writing and using Classes effectively.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Classes and Objects in Java
26-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
PHP Workshop ‹#› PHP Classes and Object Orientation.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Abstract classes and Interfaces. Abstract classes.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
4.1 Instance Variables, Constructors, and Methods.
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.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Programming in Java CSCI-2220 Object Oriented Programming.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
Copyright Curt Hill Variables What are they? Why do we need them?
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
CET203 SOFTWARE DEVELOPMENT Session 2B Constructors, Overriding and Overloading.
CIT 590 Intro to Programming Lecture 13. Agenda A few topics that you have seen but might not have fully grasped Static Public, private, protected etc.
PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?
Cases and Classes and Case Classes And Other Miscellany 16-Dec-15.
M1G Introduction to Programming 2 5. Completing the program.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Classes and Objects and Traits And Other Miscellany 25-Jan-16.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
OOP Basics Classes & Methods (c) IDMS/SQL News
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
BY:- TOPS Technologies
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Abstract Classes and Interfaces
Object Oriented Programming (OOP) LAB # 8
CS1316: Representing Structure and Behavior
Week 6 Object-Oriented Programming (2): Polymorphism
CS1316: Representing Structure and Behavior
The Scala API.
Java Programming Language
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Presentation transcript:

More about Classes and Objects

Names Scala has three kinds of names for variables and methods Names composed of letters, underscores, and dollar signs (but you shouldn’t use dollar signs) scala> val simpleName = 5 simpleName: Int = 5 Names composed of “punctuation marks” (not including parentheses, brackets, braces, or the period) scala> = Int = 10 Names composed of letters and underscores, then an underscore, then punctuation marks scala> val letters_#^&* = 15 letters_#^&*: Int = 15 2

Use of names Suppose you wanted to implement your own Fraction class This works: scala> case class Fraction(num: Int, denom: Int) { | def multiply(that: Fraction) = | new Fraction(this.num * that.num, this.denom * that.denom) | } defined class Fraction scala> val f = new Fraction(1, 3) f: Fraction = Fraction(1,3) scala> f.multiply(f) res0: Fraction = Fraction(1,9) But this is nicer: scala> case class Fraction(num: Int, denom: Int) { | def *(that: Fraction) = | new Fraction(this.num * that.num, this.denom * that.denom) | } defined class Fraction scala> val f = new Fraction(1, 3) f: Fraction = Fraction(1,3) scala> f * f res2: Fraction = Fraction(1,9) 3

Misuse of names A lot of methods in Scala have names composed of “punctuation marks” For example, ==, >=, +: Used carefully, such names can increase the readability of your program But a name is probably never good! “Just because you can do something, doesn’t mean you should!” 4

What’s this ? Objects contain variables ( val s and var s) and methods You can explicitly say “I want the variable in this object” You can do so with the keyword this : scala> case class Fraction(num: Int, denom: Int) { | def *(that: Fraction) = | new Fraction(this.num * that.num, | this.denom * that.denom) | } But you can also implicitly refer to variables in “this” object Unqualified variables are first looked for in the local scope, then among the variables of “this” object scala> case class Fraction(num: Int, denom: Int) { | def *(that: Fraction) = | new Fraction(num * that.num, denom * that.denom) | } defined class Fraction 5

More about this An instance method is a method that belongs to an object To “call” an instance method, you are asking the object to execute the method for you The keyword this acts like a variable whose value is the object executing the method There are times you want your method to “call” some other method, and tell the method who is “calling” it Use the keyword this as an argument to the other method 6

Companion objects When you declare an object, you are actually doing two things: You are creating a class You are creating the one and only one object of that class When you declare a class, you are making a template from which to create zero or more objects Every object has its very own copies of the variables declared in that class Sometimes, however, you want some variables whose value is the same for every object of that class This is the purpose of a companion object A companion object is an object with the same name as a class, and defined on the same file as the class 7

Example companion object You want to describe a number of janitors Every janitor gets the same salary Solution: object Janitor { var salary: Double = } class Janitor(name: String) 8

Inheritance When one class extends (inherits from) another class, it gets all the variables and methods defined in that class class Mammal { val hasBackbone = true val bloodTemperature = "warm" def nurse { println("Giving milk") } } class Dog(name: String) extends Mammal { def makeSound { println("Arf! Arf!") } A dog: Has a backbone, has warm blood, can nurse, and can make a sound 9

Overriding methods If your class inherits a method that you don’t like, you can “override” it (keyword override ) with another version class GirlDog extends Dog class BoyDog extends Dog { override def nurse { } // Nothing happens! Every class implicitly extends Any, or explicitly extends a class that (sooner or later) extends Any Every class inherits a toString method from Any print and println use toString when printing objects Therefore, any object can be printed—using the inherited toString method If you don’t like the results, you can override toString (Within the Dog class) override def toString = name 10

Base class initialization Whales are mammals, but they don’t have legs We might want to add a legCount variable to our consructor for Mammals class Mammal(legCount: Int) { … } Now we cannot do this: class Dog(name: String) extends Mammal { … } class Whale extends Mammal { … } We have to do this instead: class Dog(name: String) extends Mammal(4) { … } class Whale extends Mammal(0) { … } Whenever we create an object, what really happens is: We create an object of the superclass type We extend the object with anything declared in our class 11

The End 12

For each “clicker” question, I’ve been giving you 20 seconds to answer A. That isn’t enough time B. That’s usually about right C. That’s too long, in general 13