Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes & Objects: Examples

Similar presentations


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

1 Classes & Objects: Examples
15 Classes & Objects: Examples

2 Previously Methods Arguments Return Types Constructors

3 Review Classes consist of: Instances of classes are called objects
Data Variables / members / properties Functionality Methods - which 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

4 Overview Data Hiding Access Control Class Methods Abstract Classes
Understanding Hello World

5 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)

6 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.

7 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.

8 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.

9 Understanding Hello World
This class is called Hello The main method is a class method (i.e. it can be accessed without creating an instance of Hello) The main method is public (i.e. accessible outside of the class Hello) The main method does not return any data The Hello class contains a method called main 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!”); }


Download ppt "Classes & Objects: Examples"

Similar presentations


Ads by Google