Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Objects

Similar presentations


Presentation on theme: "Introduction to Objects"— Presentation transcript:

1 Introduction to Objects
We are gong to start with an overview of objected-oriented principles in order to show you the big picture. The information we manage in a Java program is either represented as primitive data or as objects. We’ll start with objects since objects are the building blocks of object oriented programing and get to primitive data a little later.

2 Introduction to Objects
An object is a basic part of a Java program An object represents something with which we can interact in a program An object provides a collection of services that we can tell it to perform for us A software object often represents a real object such as a bank account Every object has: a “state” a set of “behaviors” An object is defined by a class, which is like the data type of the object. A class is the model or blueprint from which an object is created. It establishes the kind of data an object of that type will hold and defines the methods that represented the behavior of such objects or the operations that can be performed on them. By “state” we mean state of being which are basic characteristics that currently define the object. For example, part of a bank account’s state is its current balance. The behaviors of an object are the activities associated with the object. Behaviors associated with a bank account probably include the ability to make deposits and withdrawals.

3 Using Objects The services are defined by methods in a class that defines the object A class represents a concept, and an object represents the embodiment of a class A class can be used to create multiple objects

4 Class  Object Here is a visual representation of a class and objects.
The car class is simply a blueprint or an idea from which objects can be created. A class is not an object no more that a blueprint is a house. A class contains no space to store data. Each object has space for its own data, which is why each object can have its own state. From that class blueprint, multiple objects can be created. From the car class, we were able to create a Mercedes object, a BMW object, and an Audi object.

5 Example of Using the System.out Object
The System.out object represents a destination to which we can send output In the Lincoln program below, we invoked the println method of the System.out object: System is a class in the java.lang package. out is a static member of the System class, and is an instance of java.io.PrintStream. println is a method of java.io.PrintStream. This method is overloaded to print message to output destination, which is typically a console or file. The println method will print either a string literal such as “Whatever you are, be a good one.” as shown in the example above or a variable in the parameter and then advances the program to the next line. Advancing the program to the next line is what the “ln” in print line does.

6 The print Method The System.out object provides another service as well The print method is similar to the println method, except that it does not advance to the next line Therefore anything printed after a print statement will appear on the same line

7 Character Strings Every character string is an object in Java, defined by the String class Every string literal, delimited by double quotation marks, represents a String object A character string is an object in Java, defined by the class String. Because strings are such an important part of computer programming, Java provides something called a String Literal, which appears inside double quotation marks, as we’ve seen in previous examples. We’ll explore string literals in more depth a little later.

8 String Concatenation The string concatenation operator (+) is used to append one string to the end of another It can also be used to append a number to a string A string literal cannot be broken across two lines in a program

9 A character string within in double quotation marks cannot be split between two lines of code. One way to get around this problem is to use the string concatenation operator, the plus sign . String concatenation adds one string to another.

10 We can also use the string concatenation operator to combine text and numbers. Note that the numbers in the last couple of lines are not enclosed in double quotes and are therefore not character strings. In these cases, the number is automatically converted to a string, and then the two strings are concatenated. Digits are characters and can be included in strings as needed.


Download ppt "Introduction to Objects"

Similar presentations


Ads by Google