Docsity

Classes & Objects, Study notes of Programming Languages

Partial preview of the text

Download Classes & Objects and more Study notes Programming Languages in PDF only on Docsity!

Classes & Objects

CMSC 202

Programming & Abstraction

  • All programming languages provide some form of abstraction. - Also called information hiding - Separating how one uses a program and how the program has been implemented
  • Procedural Programming
    • Data Abstraction – using data structures
    • Control Abstraction – using functions
  • Object Oriented Languages
    • Data and Control Abstraction – uses classes

What is a Class?

  • From the Dictionary
    • A kind or category
    • A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common
  • From an Object Oriented Perspective
    • A group of objects with similar properties, common behavior, common relationships with other objects, and common semantics
    • We use classes for abstraction purposes.

Classes

  • Classes are “blueprints” for creating a group of objects. - A bird class to create bird objects - A car class to create car objects - A shoe class to create shoe objects
  • The blueprint defines
    • The class’s state/attributes as variables
    • The class’s behavior as methods

Class Interface

  • The requests you can make of an object are determined by its interface.
  • Do we need to know how bagels are made in order to buy one? - All we actually need to know is which bakery to go to and what action we want to perform.

Bakery Class Is the bakery open/closed? Buy bread Buy bagel Buy muffin Buy coffee …

Type

Interface

Implementation

  • Code and hidden data in the class that satisfies requests make up the class's implementation. - What’s hidden in a bakery?
  • Every request made of an object must have an associated method that will be called.
  • In OO-speak we say that you are sending a message to the object, which responds to the message by executing the appropriate code.

Class Definition

  • A class definition defines the class blueprint.
    • The behaviors/services/actions/operations of a class are implemented methods. - Also known as member functions
    • The state of the class is stored in its data members.
      • Also known as fields , attributes , or instance variables
  • A challenging aspect of OOP is determining what classes get modeled and at what level of detail. - This answer will vary based on the problem at hand.

Objects

  • Remember an object is a particular instance of an a class.
  • As such, all objects have…
    • Data Members
      • The variable types and names (same across all instances)
      • The members of each object can hold different values (unique to that instance).
      • The state of an object is defined by these values.
    • Methods
      • The tasks that the object can perform (same across all instances)

Data Members

  • Objects store their individual states in “non- static fields” known as data members.
  • Primitive types or reference types
  • Accessible by all methods of the class
    • Thus the members are said to have class scope.
  • Members are referenced using the dot operator … numItems = array.length;

Anatomy of Class Data Members

public class Bakery {

}

  • Remaining class body (methods)

public boolean closed ; int numBagels ;

Optional access modifier (more on this later)

Data member type

Data member name

Methods

  • Objects are sent messages which in turn call methods.
  • Methods may be passed arguments and may return something as well.
  • Methods are available to all instances of the class.
  • Like data members, methods are also referenced using the dot operator … System.out.println(name.charAt(0));

Anatomy of a Method

Optional access modifier (More on this later)

Name of method

return type (may be void)

public int^ getBagelCount {

}

Method code: local variables and statements

()

Optional parameters

Creating a Car

  • The following defines a reference variable of type Car.
    • However there is no Car object yet!
  • The statement myCar = new Car( ) creates a “new” Car object and associates it with the variable “myCar”. - Now “myCar” refers to a Car object.
  • For convenience, these statements can be (and are typically) combined.

Car myCar;

myCar = new Car();

Car myCar = new Car();

Car Example

public static void main(String args[]) {

Car myCar = new Car(); myCar.vin = "123567890ABCDEF"; myCar.numLiters = 2; myCar.horsepower = 195; myCar.year = 2008; myCar.changeColor("Black"); System.out.println("Car is colored: " + myCar.color); System.out.println("Car is " + (2011 - myCar.year) + " years old");

}