Title: Using classes as objects
1Using classes as objects
2- // header
- public class MainClass
-
- public int y
- public static void main(String args )
-
- int x
- supportMethod( )
- // end main method
- public static supportMethod( )
-
- // end supportMethod
- // end Lab1MainClass
Fully independent class definition shared
variables main method not-shared method
call support method note end brackets
3Why independent
- Has a main( )
- Does not use other classes
- Not very powerful
4What is a class?
- A program, with no main
- Contains useful methods and variables, that you
might want to use in your program, that HAS a
main.
5Instantiation
- You or someone else writes a class
- public class someClass
- ..
- You use the class in your program
- You must make a local copy
- someClass myObject new someClass( )
- Or, two-step
- public someClass myObject
- myObject new someClass()
6What a Class does
- A collection of methods and variables.
- Every Object created from a class, operates as an
independent, freely running program. - Only one main allowed
- main spawns useable objects, sometimes many
objects of the same class.
7Using a library class
- import javax.swing.JOptionPane
- import javax.swing.JButton
- public class test
-
- public static void main (String args )
-
- JOptionPane.showMessageDialog(null, Hello)
- JButton myButton new JButton( )
-
8Another example of using a Class What if we need
a Date
- Look for a Date (or something like it) class.
- Check which library to import
- See if it has appropriate methods.
9- import java.util.Date
- public class testpublic test( ) // a
constructor! System.out.println("Starti
ng test program") Date myDate new
Date() System.out.println( myDate )
public static void main(String args
) test app new test( )
10- // header
- import java.util.Random
- public class MainClass
-
- public int y
- public Random myGen
- public static void main(String args )
-
- int x
- myGen new Random()
-
- // end Lab1MainClass
Using a library shared variables
? Not-shared variable
?
11- import java.util.Random
- public class test1
-
- public Random myGen // creation
- public test1( ) //constructor
-
- System.out.println(test1 starting)
- myGen new Random() // binding
- // same as Random myGen new Random( )
-
- public static void main(String args )
-
- test1 app new test1()
-
- // end test1
12Writing Programs
Memory space
The Java libraries
Your program main
10,000s of classes to choose from
millions of methods
new
objects
13What if Libraries arent sufficient?
Memory space
The Java libraries
Your program main
10,000s to choose from
millions of methods
Write your own class
new
objects
14What we know about Objects
- Classes are of no value until
- They are instantiated
15Objects
- They are instantiated from Classes
- An object, is a Class that has been put to
work. - Since Classes can be used many times in the same
program, each instance of a Class is a
separate, usable copy.
16Copies of Classes
- Copy is an oversimplification, but the concept
has run-time value - A copy is created using the new statement.
- A copy of a Class is an Object.
- non-Static Classes can never be used directly.
- They must be copied to Objects
- The copy step is called instantiation.
- Your program owns and controls the copy.
17Instantiation
- Once a class is defined, making it your own for
use, using the new keyword - myClass myObject new myClass( )
- Creates an instance of the Class.
- the Class solves a Class of problems.
18Class constructor
- If a class contains a method that has the same
name as the class, it gets run automatically at
instantiation. - e.g. MyClass MyObject new MyClass( )
- will run a method in MyClass, if its name
MyClass( ) - Called the Class Constructor.
- It's the reason for the parenthesis in the
instantiation statement
19Applications - The main Method
- The computer runs it, you dont.
- The computer calls the main method
- The main method uses methods, contained in
Objects, derived from Classes that you write.
20- public static void main(String args)
-
- ShowColors app new ShowColors( )
- // any method in ShowColors is now useable
- // as app.MethodName( )
21Introducing, the JFrame
- Gives us a work area beside System.out.println
22Three ways to make use of classes
- One step instantiation
- JFrame myWindow new JFrame( )
- Two step Instantiation
- public JFrame myWindow
- public static void method( int a)
-
- myWindow new JFrame( )
-
-
23third way
- public class myClass extends JFrame
-
- // your class inherits all of JFrames
- // methods
24Extension, Hierarchy, and Inheritance
25Extend keyword
- public class myClass extends someOtherClass
-
- // I can use all the methods and
- // variables from someOtherClass in myClass.
-
26Other terms
- Parent / Child
- Base class / Sub class
- Super Class / Sub class
- Any class (not just an imported library class)
can be a parent or base class. - Write classes then write new inherited classes
for targeted capabilities.
27- import javax.swing.
- public class MainClass extends JFrame
-
- // stuff here
- public static void main (String args)
-
- MainClass application new MainClass( )
-
28What is this?
- Main method instantiates the class that its in?
- Because main is not considered a member of the
class that contains it. - The Main class serves a place-holding function
for the computer, by keeping main in a usable
place. - Instantiating the Main class causes the
constructor (if there is one) to be run. - What other reasons?
29JFrame
- Puts graphics-capable windows on screen.
- JOptionPane uses JFrame
- Colors
- Fonts
- Drawings
- A frame for intuitive GUI
- Adds a paint method
30Use these helper libraries
- import javax.swing.JFrame // for JFrame
- import java.awt. // for painting and animation
- import java.awt.event. // for event
handling, more later - import java.util.Random // always useful
31some JFrame methods
- setLocation(100,200)
- setSize(250,250)
- setVisible(true)
- setDefaultCloseOperation( EXIT_ON_CLOSE)
32- public class MainClass extends JFrame
-
- public MainClass ( )
-
- setLocation(100,200)
- setSize(250,250)
- setVisible(true)
- // end MainClass constructor
- public static void main (String args)
-
- MainClass application new MainClass( )
- // end main method
- // end class
33- public class MainClass extends JFrame
-
- public MainClass ( int x, int y )
-
- setLocation( x, y )
- setSize(250,250)
- setVisible(true)
- // end MainClass constructor
- public static void main (String args)
-
- MainClass application new MainClass(100,
200) - // end main method
- // end class
34- public class MainClass extends JFrame
-
- public MainClass ( int x, int y )
-
- setLocation( x, y )
- setSize(250,250)
- setVisible(true)
- // end MainClass constructor
- public static void main (String args)
-
- MainClass appl1 new MainClass(100, 200)
- MainClass appl2 new MainClass(200, 300)
- // end main method
- // end class
35Demo
36If you dont extend JFrame.
- public class MainClass
-
- public MainClass ( )
-
- JFrame myFrame new JFrame( )
- myFrame.setLocation(100,200)
- myFrame.setSize(250,250)
- myFrame.setVisible(true)
- // end MainClass constructor
- public static void main (String args)
-
- MainClass application new MainClass( )
- // end main method
- // end class
37Objects As Containers of Information
38Objects dont always DO things
- Sometimes they just HOLD things (information)
- The Color class can hold color information
- The Point class can hold cartesian coordinate
information
39Color class
- Like the String class, and Integer class, does
not use the new keyword. - holds a color value (nothing more)
- e.g Color boxColor new Color( )
- boxColor Color.blue
- or
- Color boxColor Color.blue
- then boxColor can be used to set System
properties in Classes/Objects that need color
(more later).
40JColorChooser returns a Color object to the
caller
41Returns an Object?
- JColorChooser fills in all of the information in
a blank object of the Color class, and copies it
to the Color object in the calling statement - boxColor JColorChooser.showDialog(
- null, Greeting, default color )
42the this qualifier
- Means this object, the one that were in.
- Used when a call to a method, especially in a
child, needs to specify which object the method
should act upon. - this always refers to an object of some type.
43the super qualifier
- refers to the parent class
- the command super calls the parents constructor
- super.MethodName can call any method in the
parent.
44A Random Number generator
- import java.util.Random
- .
- .
- .
- Int X 0
- Random myGen new Random( )
- X myGen.nextInt( 50 )
45Meaning
- myGen is an Object of the Random class.
- It has methods.
- The nextInt( num ) method returns an int
between 1 and num.
46graphics on your computer
- Since there are many computers (types,
instances).... - and since the programs that you write should run
anywhere.... - then the programs that you write, should retrieve
and use information native to the computer that
it is running on. - This is called context awareness
47The paint Method
- Computer runs it once (you dont), but then you
can run it by calling repaint(). - The computer passes it an object of the Graphics
Class, of its own making.
48- public void paint (Graphics g)
-
- // the computer calls the paint program
- // automatically whenever an event requires
- // the screen to be redrawn
49Graphics!
- g.setColor
- g.fillRect
- g.drawString
- g.drawPolygon
- hundreds of methods that use your computers
graphics capability
50demo
- A Frank Lloyd Wright style generator
51what does this do?
- FlowLayout layout new FlowLayout( )
- setLayout( layout )
52Setting up a JFrame
- setSize( w, h )
- setLocation( x, y )
- setDefaultCloseOperation( EXIT_ON_CLOSE )
- FlowLayout layout new FlowLayout()
- setLayout( layout )
- JButton helloButton new JButton( "Hello" )
- add( helloButton )
- setVisible( true )