Download presentation
Presentation is loading. Please wait.
Published byErin Clark Modified over 9 years ago
1
Introduction To Object-Oriented Programming
2
Object-Oriented Programming Class: Code that defines the behavior of a Java programming element called an object Object: An entity that has both state and behavior State: Any data that the object might be keeping track of Behavior: Actions that the object can perform
3
Object-Oriented Programming The Difference Between Class and Object Class – A plan for an object Object – An instance of a class Blueprint – A plan for a house House – An implementation of a blueprint
4
Object-Oriented Programming The Difference Between Class and Object Class – A plan for an object Object – An instance of a class – When an object is created, Java sets aside computer memory sufficient to hold the data stored by the object. – Each instance of a class has its own data independent of the data used by other instances of the same class
5
Object-Oriented Programming Understanding Static Methods You do not always have to create an instance of a class to use the methods of a class.
6
Object-Oriented Programming Understanding Static Methods public class HelloApp { public static void main (String [] args) { System.out.println ("Hello, World!"); } Static can call the method without first creating an instance of the class. Static methods are called from classes, not from objects.
7
Object-Oriented Programming Understanding Static Methods public class HelloApp { public static void main (String [] args) { System.out.println ("Hello, World!"); } Java does not create an instance of the application class. Instead, it calls the program’s static main method.
8
Object-Oriented Programming Understanding Static Methods – Analogy Time! A blueprint includes details about systems (electrical, water pipes etc.) but you can not turn on the hot water without building the house. A blueprint can be used to give you dimensions of different rooms. If a blueprint had a built-in calculator that displays the size of the living room when a the Living Room Button is pressed it would be like a static method in a class. – You can activate it from the blueprint alone. Many java programs are entirely made up of static methods though more advanced programs rely on one or more objects.
9
Creating An Object From A Class Create a variable that provides a name you can use to refer to the object – Use the Keyword new to create an instance of the class – Assign the resulting object to the variable ClassName variableName = new ClassName ( );
10
Creating An Object Class1 myClass1Object = new Class1 ( ) ; Creates an object instance of a class named Class1 Assigns Class1 to a variable named myClass1Object
11
Creating An Object Class1 myClass1Object = new Class1 ( ) ; Class name twice? – First time provides a type for the variable. You are saying that the variable you are creating here can be used to hold objects created from the Class1 class – Second time you are creating an object from the class. new tells Java to create an object. The class name provides the name of the class to use to create the object = is an assignment operator – Says to take the object created by the new keyword and assigns it to the variable.
12
Creating An Object Class1 myClass1Object = new Class1 ( ) ; This statement accomplishes 3 things: 1.Creates a variable named myClass1Object that can hold objects created from the Class1 class.
13
Creating An Object Class1 myClass1Object = new Class1 ( ) ; This statement accomplishes 3 things: 2. Creates a new object in memory from the Class1 class
14
Creating An Object Class1 myClass1Object = new Class1 ( ) ; This statement accomplishes 3 things: 3. Assigns this newly created object to the myClass1Object variable. You can now use the myClass1Object variable to refer to the object that was created.
15
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; } Java requires that each public class be stored in a separate file with the same name as the class
16
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } End-of-line comments explain the functionality of the program and how it works.
17
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } Begins the HelloApp2 class with the public class-declaration Since the public keyword is used, a file named HelloApp2.java must contain this class
18
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } A main method is declared as it is in ALL java programs
19
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } The body of the main method creates a variable named myGreeterObject that can hold objects created from the Greeter class. Creates a new object using the Greeter class and assigns this object to the myGreeterObject variable
20
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } Calls the myGreeterObject object’s sayHello method (this method in Greeter.java displays “Hello, World!”)
21
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; }
22
Viewing A Program That Uses An Object Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; } Starts with end-of-line comments that identify the function of the program
23
Viewing A Program That Uses An Object Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; } Declares a public class declaration. Allows HelloApp2 class to access Greeter class
24
Viewing A Program That Uses An Object Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; } sayHello method is declared using the public keyword so that it is available to other classes that use the Greeter class. void keyword indicates that this method does not provide any data back to the class that calls it. sayHello provides the identifier for the method
25
Viewing A Program That Uses An Object Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; } Body of method consists of one statement. Displays “Hello, World!” on the console
26
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console public class Greeter { public void sayHello ( ) { System.out.println (“Hello, World!”) ; } Defines a class that knows how to say “Hello, World!” Creates an object from that class and asks that object to say “Hello, World!” The application does not know exactly how the Greeter object displays on the console, what it will display, or how it will be displayed
27
Viewing A Program That Uses An Object HelloApp2.java // This application displays a hello message on // the console by creating an instance of the // Greeter class and then calling the Greeter // object’s sayHello method. public class HelloApp2 { public static void main (String [ ] args) { Greeter myGreeterObject = new Greeter ( ) ; myGreeterObject.sayHello ( ) ; } Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console import javax.swing.JOptionPane; public class Greeter { public void sayHello ( ) { JOptionPane.showMessageDialog (null, >, >, JOptionPane.INFORMATION_MESSAGE); } Accomplishes the same thing by making the Greeter class to use a Java library class No changes are required for HelloApp2.java
28
Importing Java API Classes Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console import javax.swing.JOptionPane; public class Greeter { public void sayHello ( ) { JOptionPane.showMessageDialog (null, >, >, JOptionPane.INFORMATION_MESSAGE); } import keyword to lets the compiler know that the program is using a class that is defined by the Java API imported class is called JOptionPane import trivia: import statements must appear at the beginning of a class file, even before any class declarations You can include as many import statements as are necessary to import all the classes used by your program import javax.swing.*; This statement can be used to import all classes from a particular package
29
Importing Java API Classes Greeter.java // This class creates a Greeter Object // that displays a hello message on // the console import javax.swing.JOptionPane; public class Greeter { public void sayHello ( ) { JOptionPane.showMessageDialog (null, >, >, JOptionPane.INFORMATION_MESSAGE); } The Java API is full of thousands of classes Java groups classes into manageable groups called packages JOptionPane is contained within the javax.swing package
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.