File

advertisement
Object Oriented Programming
Object and Classes
Lecture 3
MBY
What are Objects?
• Building blocks of a software
– A set of cooperating objects that work together by
sending messages to each other
• Object model tangible things
– school
– Car
• Objects model conceptual things
– meeting
– date
• Objects model processes
– finding a path through a maze
– sorting a deck of cards
What does an Object have?
• Objects have
– capabilities: what they can do, how they
behave. Capabilities are also know as
behavior. Capabilities include constructors
commands and queries.
– properties: features that describe them
Properties can be attributes, components,
associations.
Example of Capabilities
• Example: Jugs are capable of performing
specific actions
- constructor: be created.
- commands: add water, empty yourself.
- queries: reply whether lid is open or closed, or
whether jug is full or empty.
Examples of Properties and object
state
• Example: Jugs contain the following properties
- attributes: color, material, smell
- components: lid, container
- associations: a jug can be associated with the
room it’s in.
• Object State: collection of all of an object’s
properties; changes if any property changes
- some don’t change, e.g., steering wheel of car
- others do, e.g., car’s color
Classes
• Object class
– a class is a category of object
– defines capabilities and properties common
among a set of individual objects
• all trash cans can open, close, empty their trash
– defines template for making object instances
• particular trash cans may have a metal casing, be
blue, be a certain size, etc.
Classes
• Classes implement capabilities as methods
– a method is a sequence of statements in Java
– objects cooperate by sending messages to others
– each message “invokes a method”
i.e., Java executes the sequence of statements in the method in response to
a message.
• Classes implement properties as instance
variables
– slot of memory allocated to the object that can
hold a potentially changeable value
Example of class
String city = “Lahore”;
– Why the “S” in String is not in small caps like
int, double, etc.
– String is an existing Java class and it is used to
store values specified as strings.
– As a rule of thumb in Java anything specified in
double quotes “ ” is a String.
Syntax for Using Existing Classes
• Existing classes are instantiated into objects by this syntax.
String city = new String()
1
1
2
3
4
2
3
4
Tells the compiler to search for the definition of a class
named “String”
A unique name for the object in the memory
“new” keyword to reserve the space for the type String in
the memory
String() is a constructor of the class “String”
Constructor
• Constructor is a special method that is called
whenever a class is instantiated (created)
– another object sends a message that calls a
constructor
– A constructor is the first message an object
receives and
cannot be called subsequently
– establishes initial state of properties for instance
• Constructors have special syntax:
– must always have same name as class name
Constructors (cont’d)
• A class could have many constructors e.g.
String city = new String()
&
String city = new String(“Lahore”)
Both construct a String object but in the first
no initial value is given to the String and in
the second a value “Lahore” is given.
Welcome Program
Hello.java
class Hello {
public static void main(String[] args) {
System.out.println(“Welcome to java”);
}
}
•class is a keyword in Java language, used to define a
class.
•Hello is the name of class. The class keyword must
be followed by the its name.
•By convention name of the class must be started
with capital letter.
•Name of class can be any valid identifier.
•Normally class is given a name of a noun.
Welcome Program (Cont.)
Hello.java
class Hello {
public static void main(String[] args) {
System.out.println(“Welcome to java”);
}
• Java program must be written in form of classes.
• Program file must be save with the same name as
the class.
• main function is the entry point in Java.
• Class definition starts with opening curly bracket
and end with closing curly bracket similarly as
function/method in java.
}
What is Java Package, and API?
• Java’s numerous predefined classes are
grouped into categories of related classes
called packages.
• The packages are referred to collectively
as the Java class library, or the Java
applications programming interface (Java
API)
Core Packages and Extension Packages
• The packages of the Java API are split into core
packages and extension packages. The names
of the packages begin with either “java” (core
packages) or “javax” (extension packages).
e.g java.lang, java.util, java.io etc are core
packages whereas javax.swing are extension
packages.
Including a package
• import key word is used to include a package in
the program.
• A package must be imported in java program
before using classes contained in that package.
• import javax.swing.JOptionPane is the syntax to
import JOptionPane class in the program which is
available in java extention package, swing.
• Why did we not import any package in our first
program ?
Automatical inclusion
• Java automatically imports classes from
package java.lang, such as class System.
Therefore, import statements are not
required for classes in package java.lang.
Another Example
// An addition program.
// Java extension packages
import javax.swing.JOptionPane; // import class JOptionPane
public class Addition {
// main method begins execution of Java application
public static void main( String args[] )
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
int number1; // first number to add
int number2; // second number to add
int sum; // sum of number1 and number2
// read in first number from user as a String
firstNumber =JOptionPane.showInputDialog( "Enter first integer" );
Addition Example Cont.
// read in second number from user as a String
secondNumber = JOptionPane.showInputDialog( "Enter second integer" );
// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// add the numbers
sum = number1 + number2;
// display the results
JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE)
System.exit( 0 ); // terminate application
} // end method main
} // end class Addition
Dialogues
Following dialogues will be displayed in response to our
addition example
Download