OOP Preliminaries and Terminology - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

OOP Preliminaries and Terminology

Description:

You say to 'Hello world' (and instance of class String): 'give me your fourth character' ... You say to the String 'Hello ': concatenate the string 'world' to ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 14
Provided by: Steve57
Category:

less

Transcript and Presenter's Notes

Title: OOP Preliminaries and Terminology


1
OOP Preliminaries and Terminology
  • First some programming / Java concepts and
    terminology
  • Everything is either data or function
  • data are objects, things, nouns
  • functions are algorithms, procedures, methods,
    verbs
  • In Java we talk about objects (data) and methods
    (function)
  • for example, in the single line of code we have
    written so far
  • System.out.println(Hello world)

2
Classes and Objects
  • A Java class represents an abstract collection of
    individuals (?)
  • String the set of all possible strings
  • PlayingCard the set of all possible playing
    cards
  • Employee the set of all possible employees
  • EmailMessage the set of all possible email
    messages
  • Every object (every data object) is an instance
    of some class (?)
  • "Hello world" is an instance of class String
  • 1.25 is an instance of class Number (?)
  • '' is an instance of class Character (?)
  • System.out is a member of class PrintStream
  • for many classes, there is no pre-defined
    "literal" way to refer to an object I can't say
    "Ace of Spades" is a member of class PlayingCard
  • Any time you see an object in a program, you need
    to be aware of what its data type is!

3
Java Methods
  • A method (subroutine, procedure, function) is a
    block of Java code that does some calculation or
    makes some change
  • concatenate two strings
  • extract the 4th character from a string
  • print a line of text
  • give an employee a raise
  • extract the sender of an email message
  • change
  • A class defines all the methods for its member
    objects, and all methods are defined for some
    class, so there is no such thing as "disembodied
    calculation"
  • You can't just print a line of text to the
    console. You say to System.out, (an instance of
    class PrintStream) "print this string"
  • You can't just extract the 4th character of a
    string. You say to "Hello world" (and instance
    of class String) "give me your fourth character"
  • You can't just give an employee a raise. You say
    to an instance of Employee "increase your
    salary by 500"
  • You can't just concatenate two strings "Hello "
    and "world" to get the string "Hello world". You
    say to the String "Hello " concatenate the
    string "world" to yourself
  • If you want to get some work done, you have to
    find some object to do it for you!

4
Java Methods and Mathematical Functions
  • What programmers call methods or procedures are
    somewhat like mathematical functions
  • This definition specifies
  • the name of the function
  • how many arguments
  • what is returned
  • implicitly, the types of the arguments and the
    type of the return value
  • In programming terms, this is called the method's
    signature
  • the method's name
  • the number and data types of its arguments
    (inputs)
  • the data type of its output
  • But how would this function (minimum) work in the
    object-oriented way of thinking?
  • What is the defining class and signature of Java
    methods that
  • concatenate two strings
  • extract the 4th character from a string
  • extract the nth character from a string

5
To Repeat the Most Important Point of the Week
  • Everything is either data or procedure
  • In Java we call them objects and methods
  • Every object has a data type
  • in Java, this is often a Class
  • the objects data type determines what methods
    apply to it
  • for example, the String type supports
    concatenation and substring
  • the numeric types support operations like
    addition and multiplication
  • the PrintStream type supports the println
    operation
  • your user-defined type Employee might support the
    salary() and raiseSalary(Number) methods
  • Every method has a signature
  • the name of the method
  • the names and data types of the arguments
  • the data type of the return result
  • In the OOP way of computing, all computation is
    accomplished by doing a method call on an object

6
Classes, Objects, and Methods
Class String
Method concat(aString) substring(from,
to) charAt(position)
class constructs an instance
instance receives method calls "Hello
world".charAt(0)
Instance "Hello world"
7
Creating Objects
  • Always keep in mind the distinction between the
    class, and instances of the class
  • String is a class, "Hello world" is an instance
  • Number is a class, 3.14159 is an instance (?)
  • PrintStream is a class, System.out is an instance
  • The only thing a class can do is create an
    instance of itself (?)
  • be aware of the compile time versus run time
    distinction!
  • There is only one way to create an instance of a
    class, via one of the class's constructors
  • different classes have different constructors
    many classes have more than one constructor

8
Various Forms of the Constructor
  • Classic constructors that "make sense"
  • new String() // constructs an instance of
    String with 0 characters
  • new PlayingCard("ace", "diamonds")
  • new Employee(47, "John Smith", "Software
    Engineer", 85000)
  • Constructors that you're not likely to see!
  • new String("Hello world")
  • new Character('A')
  • new Float(3.14159)
  • Why won't you see any of the constructors above
    in your Java code?

9
Literals
  • Certain fundamental Java classes have literals
    associated with them they do the same thing as
    the classic constructor, but are easier (ONLY)
    for the sake of your convenience and code
    readability
  • "Hello world" is exactly the same as new
    String("Hello world")
  • 3.14159 is exactly the same as new
    Float(3.14.19) (?)
  • '' is exactly the same as new Character('')
    (?)
  • true is exactly the same as new Boolean(true)
    (?)
  • Of course you're going to use the literal form of
    the constructor when you can just be aware that
    it really is a shortcut, and the fundamental
    process here is that of a class creating an
    instance of itself.

10
Summary
  • All computation is about data and function
  • data holds state (a number, a string, an
    Employee)
  • functions perform calculations or produce or
    change values
  • add two numbers, extract the 3rd character in a
    string, change an employee's salary
  • in Java, data classes, function methods
  • In Java and other object-oriented languages
    classes
  • are collections of individuals (Employee,
    PlayingCard, String)
  • a class versus instances of that class
  • define methods that all instances can respond do
  • the PlayingCard class defines the getSuit method
  • the Employee class defines the setSalary method
  • the String class defines the concatenate method
  • Although a class defines the methods, members of
    the class actually respond to the method calls
  • aPlayingCard.getSuit()
  • anEmployee.setSalary(100000)
  • "Hello".concat(" world")

11
Summary (cont.)
  • The connection between a class and instances of
    the class the constructor
  • literals (constructors for a few Java-defined
    types)
  • String "hello"
  • numbers 12 3.14
  • boolean true false
  • char 'a'
  • explicit constructor
  • new Employee(123, "Joe", 100000)
  • new PlayingCard("Ace", "Clubs")
  • new Scanner(System.in)
  • new Calendar()
  • classes can have multiple constructors

12
Summary (cont.)
  • A class defines
  • its constructors (all the ways you can create an
    instance)
  • methods that all instances respond to
  • A method is described by its signature
  • its name
  • its arguments
  • its return result
  • for example the String class defines
    charAt(position)
  • the method is defined in class String, which
    means that all instances of String (and only
    instances of String) can respond to this method
  • the method's name is charAt
  • it has a single argument, and the argument's data
    type is int
  • its return result is char
  • To put it all together, explain what happens here
  • System.out.println("Hello world".charAt(5))

13
Summary (cont.)
  • Since a method is defined by its signature
    (arguments and their types, return type, and
    name) there can be multiple methods that happen
    to have the same name.
  • You have to be careful because although your
    intuition says that two methods must somehow be
    the same if they have the same name, Java says
    that they must be different methods if their
    signatures differ in any way.
  • and Java always wins these arguments
  • One example we will see frequently
  • System.out.println("Hello")
  • System.out.println(3.25)
  • System.out.println()
Write a Comment
User Comments (0)
About PowerShow.com