Presentation is loading. Please wait.

Presentation is loading. Please wait.

Examples of Classes & Objects

Similar presentations


Presentation on theme: "Examples of Classes & Objects"— Presentation transcript:

1 Examples of Classes & Objects
G6DICP - Lecture 13 Examples of Classes & Objects

2 Revision Classes consist of: Instances of classes are called objects
Data Variables / members / properties Functionality Methods Methods optionally accept arguments and return values Instances of classes are called objects Instance Variables / Methods The default One copy for each instance of the class Class Variables / Methods static One copy shared by all instances of the class

3 Data Hiding Class and instance variables can be accessed from anywhere providing the full object or class name is used. Variables declared in a method are “hidden” local to that method (scope and scoping)

4 Access Control Methods, classes and variables may be given modifiers to indicate how they may be used. static - used to indicate a class variable public private Public Methods This is the default. Public methods may be used anywhere (providing the full class/object name is used). Private Methods Access is restricted to the class that contains the method.

5 Class Methods Class methods (like class variables) have the static modifier. This means that they can be used without instantiation. static int squared(int i) { return i*i; } Classes that are not allowed to be instantiated are abstract classes.

6 Abstract Classes Abstract classes are declared with the abstract modifier. Abstract classes may not be instantiated. Abstract classes serve to hold static variables and methods. This is often useful for libraries of predefined functionality.

7 Understanding “Hello World!”
class Hello { public static void main (String[] args) System.out.println(“Hello World!”); }

8 Understanding “Hello World!”
The class is called Hello class Hello { public static void main (String[] args) System.out.println(“Hello World!”); }

9 Understanding “Hello World!”
The Hello class contains a method called “main” class Hello { public static void main (String[] args) System.out.println(“Hello World!”); }

10 Understanding “Hello World!”
The main method is public (ie accessible outside of the class Hello) class Hello { public static void main (String[] args) System.out.println(“Hello World!”); }

11 Understanding “Hello World!”
The main method is a class method (ie it can be accessed without creating an instance of Hello) class Hello { public static void main (String[] args) System.out.println(“Hello World!”); }

12 Understanding “Hello World!”
The main method does not return any data. class Hello { public static void main (String[] args) System.out.println(“Hello World!”); }

13 Understanding “Hello World!”
The main method has a parameter called args (an array of String) class Hello { public static void main (String[] args) System.out.println(“Hello World!”); } 8


Download ppt "Examples of Classes & Objects"

Similar presentations


Ads by Google