Using classes as objects - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Using classes as objects

Description:

Every Object created from a class, operates as an independent, freely running program. ... main 'spawns' useable objects, sometimes many objects of the same class. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 53
Provided by: msresearch
Category:
Tags: classes | objects | using

less

Transcript and Presenter's Notes

Title: Using classes as objects


1
Using 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
3
Why independent
  • Has a main( )
  • Does not use other classes
  • Not very powerful

4
What 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.

5
Instantiation
  • 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()

6
What 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.

7
Using 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( )

8
Another 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

12
Writing Programs
Memory space
The Java libraries
Your program main
10,000s of classes to choose from
millions of methods
new
objects
13
What 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
14
What we know about Objects
  • Classes are of no value until
  • They are instantiated

15
Objects
  • 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.

16
Copies 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.

17
Instantiation
  • 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.

18
Class 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

19
Applications - 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( )

21
Introducing, the JFrame
  • Gives us a work area beside System.out.println

22
Three 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( )

23
third way
  • public class myClass extends JFrame
  • // your class inherits all of JFrames
  • // methods

24
Extension, Hierarchy, and Inheritance
25
Extend keyword
  • public class myClass extends someOtherClass
  • // I can use all the methods and
  • // variables from someOtherClass in myClass.

26
Other 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( )

28
What 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?

29
JFrame
  • Puts graphics-capable windows on screen.
  • JOptionPane uses JFrame
  • Colors
  • Fonts
  • Drawings
  • A frame for intuitive GUI
  • Adds a paint method

30
Use 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

31
some 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

35
Demo
36
If 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

37
Objects As Containers of Information
38
Objects 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

39
Color 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).

40
JColorChooser returns a Color object to the
caller
41
Returns 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 )

42
the 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.

43
the super qualifier
  • refers to the parent class
  • the command super calls the parents constructor
  • super.MethodName can call any method in the
    parent.

44
A Random Number generator
  • import java.util.Random
  • .
  • .
  • .
  • Int X 0
  • Random myGen new Random( )
  • X myGen.nextInt( 50 )

45
Meaning
  • myGen is an Object of the Random class.
  • It has methods.
  • The nextInt( num ) method returns an int
    between 1 and num.

46
graphics 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

47
The 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

49
Graphics!
  • g.setColor
  • g.fillRect
  • g.drawString
  • g.drawPolygon
  • hundreds of methods that use your computers
    graphics capability

50
demo
  • A Frank Lloyd Wright style generator

51
what does this do?
  • FlowLayout layout new FlowLayout( )
  • setLayout( layout )

52
Setting 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 )
Write a Comment
User Comments (0)
About PowerShow.com