Evan Korth - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Evan Korth

Description:

New York University. Road Map. Introduction to object oriented programming. Classes ... field) is used to define state (attributes or properties) of the entity. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 16
Provided by: evank
Learn more at: https://cs.nyu.edu
Category:
Tags: evan | korth | map | new | of | state | york

less

Transcript and Presenter's Notes

Title: Evan Korth


1
Computer Science IClasses and ObjectsProfessor
Sana OdehNew York University
2
Road Map
  • Introduction to object oriented programming.
  • Classes
  • Encapsulation
  • Members
  • Objects
  • Constructors
  • Reading
  • Liang 5 chapter 6 6.1 6.4
  • Liang 6 chapter 7 7.1 7.4

3
Object Oriented Programming
  • Emphasis is placed on nouns or objects.
  • Nouns (objects) have properties and behaviors.
  • How do we build these objects?
  • How do we represent their properties?
  • How do we define their behaviors?

4
Classes
  • The main building blocks of Java programs.
  • Defines objects of the same type. Like a
    blueprint.

5
Classes (cont)
  • Every .java file has one or more classes. Only
    one of the classes can be a public class.
  • That class must have the same name as the .java
    file.
  • If the class has an method called main(),
    execution can begin in that class. (Therefore,
    you can test a class by adding a main method to
    it.)
  • If there are other classes in the file, they
    cannot be public classes.

6
Encapsulation
  • Encapsulation refers to the the process of
    combining elements to create a new entity.
  • You encapsulate the properties (attributes) and
    behaviors (activities) of an entity into a class.
  • Encapsulation also enables us to hide the
    implementation of a class to other classes
    (information hiding / abstraction).

7
Designing Classes
  • A class declaration includes members of the
    class.
  • A member can be either a data member or a method
    member.
  • A data member (AKA field) is used to define state
    (attributes or properties) of the entity.
  • A method member is used to define the behaviors
    of the entity.

8
Data members
  • Data members can be a primitive type or a
    reference to another object.
  • Primitive types are integer types, floating point
    types, characters and booleans. (Note an int is
    not the same as an object of type Integer)
  • The scope of a data member is the entire class,
    no matter where within the class it is declared.

More on object references in a moment
9
Default values for data members
  • 0 for all numeric type variables (including both
    floating point types and all integer types)
  • \u0000 for char variables
  • null for reference variables
  • false for boolean type variables
  • Note No default values for local variables
    (variables declared inside a method).

More on object references in a moment
10
Objects
  • An object is an instance of a class.
  • If we think of a class as a blueprint, an object
    is one model created from that blueprint.
  • You can create any number of objects from one
    class.
  • An object is distinctly identified by an object
    reference (except for anonymous objects).

11
Declaring object references
  • In order to reference an object, we need an
    object reference variable.
  • To declare an object reference variable we use
    the syntax
  • ClassName objectReferenceName
  • The above statement creates a variable
    objectReferenceName which can reference a
    ClassName object. It does NOT create an object.

12
Instantiating objects
  • In order to create an object, we use the new
    keyword along with a constructor for the class
    of the object we wish to create.
  • To refer to the object, we point an object
    reference variable to the new object.
  • objectReferenceName new Constructor()
  • The declaration and instantiation can be combined
    as follows
  • ClassName objectReferenceName new ClassName()
  • Note the name of a constructor is the same as
    the name of the class

More on constructors soon
13
Accessing Members of a Class
  • Within a class you can access a member of the
    class the same way you would any other variable
    or method.
  • Outside the class, a class member is accessed by
    using the syntax
  • Referencing variables
  • refVar.varName
  • Calling methods (sending messages)
  • refVar.methodName(params)

14
Constructors
  • Constructors are special methods that instantiate
    objects.
  • A constructor is invoked with the new operator.
  • A constructor should initialize the class
    variables. If the variables are not initialized,
    default values are used.
  • A constructor does not have a return type.
  • A constructors identifier (name) is the same as
    the class it constructs.

15
Constructors continued
  • Constructors can be overloaded but each one must
    have its own signature.
  • A constructor with fewer arguments can call a
    constructor with more arguments (we will see how
    to do this soon).
  • If no constructor is defined, a default
    constructor is automatically supplied which
    accepts no parameters. Variables are initialized
    to their default values.
  • If one constructor is explicitly defined, the
    automatic default constructor is no longer
    available. In such case, if you want a no
    parameter constructor, you must define it
    yourself.
Write a Comment
User Comments (0)
About PowerShow.com