Using Classes in Java - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Using Classes in Java

Description:

Suppose we create another package called Tools.Analysis.SignalStrength ... something cool. So why use interfaces? Nice place to store constants ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 25
Provided by: VIC56
Category:
Tags: classes | java | using

less

Transcript and Presenter's Notes

Title: Using Classes in Java


1
Using Classes in Java
  • CMSC 432
  • Shon Vick

2
Using the Package Mechanism
  • Packages are something like libraries they are
    used to organize groups of related classes.
  • Packages provide more than just source code-level
    organization because they also create an
    additional level of scope.

3
Files as Compilation Units
  • The source code for a Java class is called a
    compilation unit.
  • A compilation unit normally contains a single
    class definition and is named for that class.
  • In fact a file may contain ONLY one public class
  • The definition of a class named AnExampleClass,
    for instance, should appear in a file named
    AnExampleClass.java.
  • The Java compiler assumes much of the
    responsibility of a make utility. The compiler
    relies on the names of source files to find and
    compile dependent classes.

4
Packages
  • A class is declared to belong to a particular
    package with the package statement.
  • The package statement must appear as the first
    statement in a compilation unit.
  • There can be only one package statement, and it
    applies to the entire file

5
Example
package mytools.text class TextComponent
...
6
Using Package Names
  • Package names are form a hierarchy, in a sense ,
    using dot-separated names.
  • Note that there is no such thing as a subpackage
    - the package name space is really flat, not
    hierarchical
  • Package names are used for the compiler and other
    run-time systems to locate files
  • Packages under a particular part of a package
    hierarchy are related only by their place in the
    hierarchy.

7
Example
  • Suppose we create another package called
    Tools.Analysis.SignalStrength
  • Those classes would not be considered in any real
    way part of the Tools.Analysis package and would
    have no special access to its members.

8
Importing Classes
  • Classes within a package can refer to each other
    by their simple names.
  • To locate a class in another package, we have to
    supply a qualifier.
  • For example we could have the fully qualified
    name of mytools.text.TextEditor.
  • Can also use an import statement.
  • One or more import statements can appear at the
    top of a compilation unit, beneath the package
    statement.
  • Like a package statement, import statements apply
    to the entire compilation unit.

9
Example
  • package somewhere.else
  • import mytools.text.TextEditor
  • class AnExampleClass
  • TextEditor editBoy
  • ...

10
Import
  • Once a class is imported, it can be referenced by
    its simple name throughout the code. It's also
    possible to import all of the classes in a
    package using the notation
  • import mytools.text.
  • Now we can refer to all public classes in the
    mytools.text package by their simple names.
  • If two different packages contain classes that
    use the same name, there can be a problem with
    importing classes that have conflicting names
  • To resolve this problem you must use fully
    qualified names to refer to those classes

11
CLASSPATH
  • The CLASSPATH environment variable must be set to
    point to any directories that contain Java
    classes that you want to import
  • To point to specific JavaPackages, you must set
    the CLASSPATH to point to the directory above the
    package
  • Example (in Unix)
  • setenv CLASSPATH./GUI/java/classes
  • (see The Java Package Tutorial)

12
How Does This Relate to Class Visibility
  • By default, a class is visible only to other
    classes within the same package.
  • This means that the class ToolsComponent is
    available only to other classes in the
    mytools.Tools package.
  • To be visible elsewhere, a class must be declared
    as public

13
Example
  • package mytools.text
  • public class TextEditor
  • ...

Missing Public Not visible
14
Inheritance in Java
  • A child class is said to extend from its parent
  • class dad
  • public void behavior()
  • //something embarrassing
  • public void looks()
  • //not that hot

class son extends dad() public void behavior()
//something not quite so bad public void
behavior(String situation) //act cool
15
Multiple Inheritance in Java
  • Java does not support true multiple inheritance
  • does support the interface construct
  • You get the method signatures from the interface,
    but you must implement all of the the methods
  • interface dummy
  • void method1()
  • class realOne implements dummy
  • void method1()
  • //something cool
  • So why use interfaces?
  • Nice place to store constants
  • Make sure your object conforms to a standard
    interface
  • Useful for composability of components

16
SubClass vs. SubTypes
  • A class is a a subclass of another class if it
    inherits from it.
  • Used to construct new software components from
    existing ones.
  • Used in OOP and frameworks.
  • A class is a subtype of another if it can be
    substituted for it with no observable change.
  • Idealized substitutability.
  • There does not have to be a relation between the
    two classes.
  • Used in component based systems.

17
Method Binding
  • By the nature of polymorphism, more then one
    implementation can be associated with a method
    invocation.
  • Binding a call to a dynamic variable leads to the
    reverse polymorphism problem
  • class Shape
  • class Square extends Shape
  • class Circle extends Shape
  • Shape A
  • Square B new Square
  • Circle C new Circle
  • A B
  • How do we know what type A is now?

18
Binding in Java
  • All subclasses are assumed to be subtypes
  • A variable can be assigned to a parent without
    explicit casting.
  • Reverse requires an explicit casting.
  • Variables are self-aware
  • They know what kind of child they are
  • This allows them to call the correct function

19
Example in Java
  • public class a
  • public void print()
  • System.out.println("This is class a")
  • public class b extends a
  • public int bnum
  • public void print()
  • System.out.println("This is class b")

20
Example Continued
public class temp public static void
main(String args) a c1 new a() b
c2 new b() a d1 b c2a
c1.print() c2.print() c2.bnum 5
d1 c2 d1.print() c2a (b) d1
System.out.println("Bnum "c2.bnum " "
c2a.bnum)
21
Memory layout
  • How much memory do we allocate to classes?
  • Required for activation records.
  • Three basic options
  • Allocate the minimum space required for the
    referenced class only.
  • Allocate the maximum amount of space needed by
    any class that can be legally assigned to the
    variable (referenced class and all its
    subclasses)
  • Allocate the amount of space necessary for a
    pointer. Allocate the space needed to hold the
    class from the heap dynamically and set the point
    as appropriate.
  • C uses the first
  • You need to create a copy operator
  • Java uses the third

22
Object Creation
  • Stack versus Heap Allocation
  • Automatic versus Dynamic Creation
  • With automatic can have handles to nothing
  • Copy handle, but space is deleted
  • With both can have memory leaks
  • Failure to clean up when object destroyed
  • Handles (pointers) versus Object
  • Indirection can be explicit or implicit
  • Gets real interesting when have distributed
    objects
  • Immutable Objects
  • Single Assignment is another name for these

23
Destruction
  • Many languages force the programmer to keep track
    when a variable is no longer needed
  • The concept of a destructor
  • Gives the programmer control of what happens when
    the resources are given back
  • Also makes for memory leaks
  • Automatic in Java
  • Can lead to some problems
  • In Java, you have to force it - Finalize
  • Implemented for Sockets and StandardFileStreams
  • Usually cleans up stuff outside control of VM
  • C destroy does much more work

24
Garbage Collection
  • As needed versus continuous
  • C does none
  • Smalltalk and Java are continuous
  • It knows when to do it better than you do
  • A counter is kept that tracks all of the the
    active references. When it goes to 0 the memory
    is freed
  • Thought question about distributed objects
  • How does it know that they are no longer needed?
Write a Comment
User Comments (0)
About PowerShow.com