34
OCA Java Basic 1

The Structure of Java Class and Java Source File

Embed Size (px)

Citation preview

Page 1: The Structure of Java Class and Java Source File

1

OCAJava Basic

Page 2: The Structure of Java Class and Java Source File

2

THE STRUCTURE OF A JAVA CLASS AND SOURCE CODE FILE

Page 3: The Structure of Java Class and Java Source File

3

THE STRUCTURE OF A JAVA CLASS AND SOURCE CODE FILE 1.1.1 Structure of a Java class 1.1.2 Structure and components of a

Java source code file …

Page 4: The Structure of Java Class and Java Source File

4

1.1.1 STRUCTURE OF A JAVA CLASS A class can define multiple components List of the components of a class:

The package statement The import statement Comments Class declarations and definitions Variables Methods Constructors

Page 5: The Structure of Java Class and Java Source File

5

1.1.1 STRUCTURE OF A JAVA CLASS Java class components

Package statementImport statementsCommentsClass declaration {VariablesCommentsConstructorsMethodsNested classesNested interfacesEnum}

Not included in OCA Java SE 7

Page 6: The Structure of Java Class and Java Source File

6

1.1.1 STRUCTURE OF A JAVA CLASSPACKAGE STATEMENT All Java classes are part of a package A Java class can be explicitly defined in a

namedpackage

Otherwise it becomes part of a default package, which doesn’t have a name

A package statement is used to explicitly define which package a class is in

If a class includes a package statement, it must be the first statement in the class definition

Page 7: The Structure of Java Class and Java Source File

7

1.1.1 STRUCTURE OF A JAVA CLASS

The package statement cannot appear within a class declaration or after the class declaration. The following code will fail to compile:

Page 8: The Structure of Java Class and Java Source File

8

1.1.1 STRUCTURE OF A JAVA CLASS The package statement must appear exactly

once in a class. The following code won’t compile:

Page 9: The Structure of Java Class and Java Source File

9

1.1.1 STRUCTURE OF A JAVA CLASS Classes and interfaces in the same package

can use each other But to use a class or an interface from

another package IMPORT STATEMENT

Page 10: The Structure of Java Class and Java Source File

10

1.1.1 STRUCTURE OF A JAVA CLASS IMPORT STATEMENT

Page 11: The Structure of Java Class and Java Source File

11

1.1.1 STRUCTURE OF A JAVA CLASS What happens if the class AnnualExam isn’t

defined in a package?

Page 12: The Structure of Java Class and Java Source File

12

1.1.1 STRUCTURE OF A JAVA CLASS Note: the import statement follows the

package statement but precedes the class declaration

Reversing this order will result in your code failing to compile

Page 13: The Structure of Java Class and Java Source File

13

1.1.1 STRUCTURE OF A JAVA CLASSCOMMENTS We can also add comments to your Java code Comments can appear at multiple places in a

class. A comment can appear

Before and after a package statement Before and after the class definition before and within and after a method definition

Comments come in two flavors: Multiline comments End-of-line comments

Page 14: The Structure of Java Class and Java Source File

14

1.1.1 STRUCTURE OF A JAVA CLASS Multiline comments: They start with /* and end with */

Multiline comments can contain any special characters (including Unicode characters)

Page 15: The Structure of Java Class and Java Source File

15

1.1.1 STRUCTURE OF A JAVA CLASS We sometime use asterisk (*) to start the

comment in the next line. This isn’t required - it’s done more for

aesthetic reason

Page 16: The Structure of Java Class and Java Source File

16

1.1.1 STRUCTURE OF A JAVA CLASS End-of-line comments : start with //, no end sign Placed at the end of a line of code. The text between // and the end of the line is

reated as a scomment

Page 17: The Structure of Java Class and Java Source File

17

1.1.1 STRUCTURE OF A JAVA CLASS A comment can precede a package statement

The multiline comment is placed before the package statement, which is acceptable because comments can appear anywhere in your code.

Page 18: The Structure of Java Class and Java Source File

18

1.1.1 STRUCTURE OF A JAVA CLASSCLASS DECLARATION The class declaration marks the start of a class It can be as simple as the keyword class followed by

the name of a class:

Page 19: The Structure of Java Class and Java Source File

19

1.1.1 STRUCTURE OF A JAVA CLASS The declaration of a class is composed of the

following parts: Access modifiers (public, private, protected) Nonaccess modifiers (final, static…) Class name Name of the base class, if the class is extending

another class All implemented interfaces, if the class is

implementing any interfaces Class body (class fields, methods, constructors),

included within a pair of curly braces, {}

Page 20: The Structure of Java Class and Java Source File

20

1.1.1 STRUCTURE OF A JAVA CLASS Example

The following list summarizes the optional and compulsory components.

Page 21: The Structure of Java Class and Java Source File

21

1.1.1 STRUCTURE OF A JAVA CLASS

Page 22: The Structure of Java Class and Java Source File

22

1.1.1 STRUCTURE OF A JAVA CLASS CLASS DEFINITION

A class is a design used to specify the properties and behavior of an object

The properties of an object are implemented using variables

and the behavior is implemented using methods. A class is a design from which an object can be

created

Page 23: The Structure of Java Class and Java Source File

23

1.1.1 STRUCTURE OF A JAVA CLASS

Page 24: The Structure of Java Class and Java Source File

24

1.1.1 STRUCTURE OF A JAVA CLASS Points to remember

A class name starts with the keyword class. (not Class)

The state of a class is defined using attributes or instance variables

The behavior is defined using methods A class definition may also include comments

and constructors NOTE A class is a design from which an

object can be created.

Page 25: The Structure of Java Class and Java Source File

25

1.1.1 STRUCTURE OF A JAVA CLASSVARIABLES Each object has its own copy of the instance

variables All properties can be call instance variables or

instance attributes. The instance variables are defined within a class

but outside all methods in a class.

Class variable or Static variable is shared by all the objects of a class

Page 26: The Structure of Java Class and Java Source File

26

1.1.1 STRUCTURE OF A JAVA CLASS METHODS

All method is call Instance methods A class method or static method is used to work

with the static variables, CONSTRUCTORS

Use to create and initialize the objects of a class. A class can define multiple constructors that

accept different sets of method parameters

Page 27: The Structure of Java Class and Java Source File

27

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE A Java source code file is used to define

classes and interfaces All your Java code should be defined in Java

source code files (file.java) Structure of a Java source code file

Definition of a class and an interface Definition of single or multiple classes and

interfaces (in the same file) Application of import and package statements to

all the classes in a Java sourcecode file

Page 28: The Structure of Java Class and Java Source File

28

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE DEFINITION OF INTERFACES

An interface is a grouping of related methods and constants

But the methods in an interface cannot define any implementation

An interface specifies a contract for theclasses to implement.

Page 29: The Structure of Java Class and Java Source File

29

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE The definition of an interface starts with the

keyword “interface”. An interface can define constants and

methods.

Page 30: The Structure of Java Class and Java Source File

30

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE DEFINITION OF SINGLE AND MULTIPLE

CLASSES IN A SINGLE JAVA SOURCE CODE FILE we can define either a single class or an interface in

a single Java source code file

Page 31: The Structure of Java Class and Java Source File

31

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE We can also define a combination of classes

and interfaces in the same Java source code file.

Page 32: The Structure of Java Class and Java Source File

32

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE

Page 33: The Structure of Java Class and Java Source File

33

1.1.2 STRUCTURE AND COMPONENTS OF A JAVA SOURCE CODE FILE

Page 34: The Structure of Java Class and Java Source File

34

IMPORT STATEMENTS IN JAVA The java source file can have multi class or interface The import statements can apply in all class or

interface

Classes and interfaces defined in the same Java source code file can’t be defined in orther packages