More on Classes - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

More on Classes

Description:

{ double x = this.x, y = this.y; //do calculation using the local x and y ... (String n, Department d) { name = n; dept = d; public Department getDepartment ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 16
Provided by: jpe897
Category:
Tags: classes | double | more

less

Transcript and Presenter's Notes

Title: More on Classes


1
More on Classes
  • Chapter 7

2
Overloading Methods
  • Ability to allow different methods or
    constructors of a class to share the same name.
  • Two methods or constructors in the same class can
    share names but they MUST have different
    signatures.
  • The compiler determines which method to call
    depending the on signature of the method
  • (parameters)
  • Operator overloading
  • Built in with primitives data types
  • Expansion allowed in C
  • Not allowed in Java

3
Examples of Overloading Methods
  • class Point
  • doubled x, y
  • Point ( )
  • x 0.0 y 0.0
  • double distance (Point other)
  • double dx this.x other.x
  • double dy this.y other.y
  • return Math.sqrt (dx dx dy dy)
  • double distance (double x, double y)
  • double dx this.x x
  • double dy this.y y
  • return Math.sqrt (dx dx dy dy)
  • double distance (int x, int y)
  • double dx this.x (double) x
  • double dy this.y (double) y
  • return Math.sqrt (dx dx dy dy)

4
Parameter Passing
  • Passed by value
  • All parameters of methods
  • Usually a method passes back a value through the
    return value
  • Passed by reference (objects)
  • Values are actually changed
  • in-out parameters
  • Final Parameters
  • Values are not allowed to be changed

5
Recursion
  • Java supports recursion
  • Recursion the attribute that allows a method to
    call itself

6
Member Modifiers
  • Public
  • Is accessible by any class
  • Protected
  • Is accessible by classes\subclasses all classes
    within the same package
  • Private
  • Is accessible only by the class itself
  • Static
  • A static field Shares all instances of the class
  • Final
  • Can not be overridden in subclasses
  • BY METHODS ONLY
  • Abstract, synchronized, native
  • BY FIELDS ONLY
  • Volatile transient

7
The main( ) Method
  • An application in Java must have a main method
  • Must be public
  • In order for the JVM (Java Virtual Machine) to
    have access to the application
  • Must be declared static
  • No object created to run application (cant be
    invoked no other way)
  • Public statis void main(String args)
  • // BODY

8
Accessing Shadowed/HiddenFields
  • public class Point
  • public double x, y
  • public Point ( )
  • public Point (double x, double y)
  • this.x x this.y y
  • Public void adjust Position ()
  • double x this.x, y this.y
  • //do calculation using the local x and y
  • //commit the changes when done
  • this.x x this.y y
  • // other methods

9
Passing Object Instances
  • public class Department
  • protected String name
  • protected Faculty facultyList new Faculty
    100
  • protected int numOfFaculty 0
  • public Department (String n)
  • name n
  • public void newFaculty (String name)
  • facultyListnumOfFaculty new Faculty (name,
    this)
  • // other methods
  • public class Faculty
  • protected Department dept
  • protected String name
  • public Faculty (String n, Department d)
  • name n dept d
  • public Department getDepartment ( )
  • return dept
  • // other methods

10
Understanding Static
  • Reserved word static
  • They can only call other static methods
  • They must only access static data
  • They cannot refer to this or super in any way

11
Understanding final
  • Reserve word - final
  • When used with variables it will not allow the
    variable to be changed
  • Similar to a constant (const) in C
  • final int num 3
  • It can be used with methods, prevents methods
    from being over righted by subclasses
    (inheritance)

12
Nested and Inner Classes
  • It is possible to define a class within another
    class.
  • A nest class has access to the members, including
    private members, of the class in which it is
    nested.
  • The enclosing class does not have access to the
    members of the nested class.
  • A class may be declared in a method as well
  • The nested and inner class should not be

13
Arrays Revisited
  • Arrays are objects
  • A member of this class is length that a
    programmer may use

class Length public static void main (String
args ) int a1 new int10 int a2
3, 5, 7, 1, 8, 99, 44, -10 int a3 4,
3, 2, 1 System.out.println(length of a1
is a1.length) System.out.println(length of
a2 is a2.length) System.out.println(length
of a3 is a3.length)
14
More on Strings
  • Strings are objects
  • Build in member
  • length
  • Java defines one operator for String
  • concatenation
  • System.out.println( 3 8 Plus some string)
    System.out.println( 3 8 Plus some
    string)
  • System.out.println(Plus some string 3 8)
  • System.out.println(Plus some string (38))

15
Command-line Arguments
  • java CommandLine this is a test 100 -1
  • class CommandLine
  • public static void main (String args )
  • for(int i0 Iltargs.length I)
  • System.out.println(args i
    argsi)
Write a Comment
User Comments (0)
About PowerShow.com