The nightmare begins''' - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

The nightmare begins'''

Description:

Example. package jeff;// This MUST be the first thing ... c1 = (Child1) p; // Allowed - type cast (will compile) but won't run ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 31
Provided by: jeffch8
Category:

less

Transcript and Presenter's Notes

Title: The nightmare begins'''


1
Chapter 2
  • The nightmare begins...

2
Warning!
  • This lecture will be, by far, the most difficult
    lecture of the class. There is a lot of material
    here that youve never seen, so it may take you
    two or three times looking at it before you
    understand it!
  • Peace,
  • -Jeff-

3
Hierarchy
  • Problem Company has different types of
    employees - Full-time, Retired, Others
  • Create class hierarchy for this

4
One Solution
Employee
Full-Time
Retired
Other
This is bad, because an employee can change
status while at the company! Youll have to
change the object that the employee is!
5
Better Solution
Employee id and status
Status
Full-Time
Retired
Other
6
Class Template
  • keywords class MyClass extends Superclass
    implements Interface, Interface, Interface
  • // class variables
  • // instance variable
  • // static initilization
  • // constructors
  • // class methods
  • // instance methods

7
Method Template
  • keywords return-type name pars
    exceptions
  • //local vars
  • // implementation

8
Packages
  • To work with a bunch of class all at once, JAVA
    allows you to group into packages.
  • Example java.awt is a package
  • Can define stricter access control
  • Easier to reuse common names
  • Collect classes so they can be shared more easily

9
Access with Packages
  • All classes that you create are put into a
    default package (whether you like it or not)
  • If not specified, all classes, methods, and
    attributes have default access
  • This means classes in the same package can access
    each others variables/method except private

10
Packages
  • Classes outside the package can only see public
    classes, and only see public or protected
    members/methods
  • Keywords public and protected do not affect
    access to classes in the same package, but
    private does.
  • There is no such thing as a private class or
    protected class its either public or default

11
In the SAME package
  • Public class Default class
  • public member yes yes
  • protected member yes yes
  • private member no no
  • default member yes yes

12
Outside the package
  • Public class Default class
  • public member yes no
  • protected member yes no
  • private member no no
  • default member no no
  • if the accessing class is a subclass

13
Packages
  • Can reuse name if in different packages
  • Specify which class you want by full name
  • java.awt.Button
  • jeff.myPackage.Button
  • Use import statement to bring in a package

14
Names
  • You can do something like this
  • java.awt.Button myButton
  • myButton new java.awt.Button (text)

15
Creating a Package
  • Use the package keyword
  • MUST BE THE FIRST THING - Even before import
    statements!!!!
  • Example package jeff.myPackage
  • This would create a directory jeff with a
    subdirectory myPackage.
  • The class would be put in /jeff/myPackage/
  • Import with import jeff.myPackage.

16
Example
  • package jeff// This MUST be the first thing
  • import java.awt.Button // Any number of
    //import statements
  • import java.util.Vector
  • class X

17
Keywords
  • public - been there, done that
  • private - that too
  • protected - yeah, yeah
  • synchronized - well see this later with Threads
  • native - means method is defined in something
    like C/C. Only used for methods

18
Other keywords
  • final - has three different meanings
  • members they cannot change after it is defined.
    final float PI 3.14159
  • method the method cannot be overriden by
    subclasses. final void bark ( )
  • class if class is final, it cannot be subclassed

19
Other Keywords
  • static - two different meanings
  • if variable is declared static, then there is
    only one variable for all instances! All
    instances share one variable (before everyone had
    their own copy)
  • if method is static, then you dont need to
    create an instance to use that method! Example
    Math.sin (45)

20
Other Keywords
  • transient - for variables only. Object cannot be
    written to a file or over the internet. For
    sensitive data, like credit card numbers.
  • transient vars cant be final or static!
  • volatile - not on exam, but used for variables
    that are in multiprocessor environments (can
    change asynchronously)

21
Other keywords
  • abstract - used on methods/classes. If a method
    is abstract, it does not implement the code, but
    lets the subclass do it!
  • If method is abstract, class must be abstract as
    well
  • Only used in inheritance
  • example Number (superclass of Byte, Short,
    Float, Double, Integer, etc)

22
Abstract Class Example
  • abstract class Bob
  • final int myInt 17
  • abstract void bark ( )
  • Note You cannot create one of these! It can
    only be subclassed!

23
Last Difficult Thing
  • We can cast from one data type to another
  • int myInt float myFloat 6.4
  • myInt (int) myFloat
  • // myInt will now contain the value 6
  • We can do the same thing with classes, up and
    down the class hierarchy

24
Assume the following
  • class Parent
  • class Child1 extends Parent
  • class Child2 extends Parent
  • class Main
  • public static void main (String args )
  • Child1 c1 new Child1 ( )
  • Child2 c2 new Child2 ( )
  • Parent p new Parent ( )
  • // Below, remember that the data type on the
    right of the assignment has to be the same as
  • // the left!
  • p c1 // Allowed, because c1 is of type
    Parent through inheritance
  • c1 p // Not allowed p is not of type
    Child1
  • c1 (Child1) p // Allowed - type cast (will
    compile) but wont run
  • c2 c1 // Not allowed - only cast up and
    down the hierarchy
  • c2 (Child2)c1 // Still not allowed - wont
    even compile

25
Danger!
  • Just because something compiles when casted
    doesnt mean it will work when it runs.
  • Working example
  • Parent p new Child1 ( )
  • c1 (Child1)p

26
Interfaces
  • An interface defines a set of abstract constants
    and methods.
  • Does not define the implementation (i.e. code)
  • Very much like an abstract class
  • Overcomes single inheritance

27
Interface example
  • public interface WildAndFunky
  • public final int myConst 42
  • public boolean pass ( )
  • public int doSomething ( )

28
How to Clone an Object
  • If you need to make a copy of an object, you must
    invoke its clone ( ) method.
  • Not all classes can be cloned, only ones that
    implement Cloneable interface
  • Doesnt call the objects constructor!
  • Button b1, b2 b1 new Button (Smile)
  • b2 b1.clone( ) // Copies all of b1 info into b2

29
Static Initialization
  • Very Powerful for showing status of loading the
    applet/application
  • These things execute while the class is loading
    (before anything else)
  • Use keyword static and curly braces
  • static
  • System.out.println (Class is loading)

30
Example
  • class Bob
  • Bob ( )
  • System.out.println (Yo)
  • static
  • System.out.println (Dude!)
  • public static void main (String args )
  • Bob b1 new Bob ( )
Write a Comment
User Comments (0)
About PowerShow.com