Title: More on Classes
1More on Classes
2Overloading 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
3Examples 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)
-
4Parameter 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
5Recursion
- Java supports recursion
- Recursion the attribute that allows a method to
call itself
6Member 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
7The 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
-
8Accessing 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
-
9Passing 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
10Understanding 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
11Understanding 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)
12Nested 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
13Arrays 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)
14More 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) -