Docsity

Class and Method Modifiers - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Class and Method Modifiers - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Class and Method Modifiers

Modifiers

  • Modifiers are Java keywords that extend or

place limits on defined classes, fields,

methods, and sub-classes.

  • Imagine that we are creating a game and want

to simulate a pair of dice. We might define a

die class, shown graphically on the next slide.

Definitions for Die

  • class – a specification of all the data, and

behavior for a single die.

  • object – a die class that has been instantiated

in a running program

  • field – a data attribute or variable like

faceValue used by the class

  • method – a specification of behavior like

roll() used by the class

Object & Class

  • Object
    • Abstracts away (data, algorithm) details
    • Encapsulates data
    • Instances exist at run time
  • Class
    • Blueprint for objects (of same type)
    • Exists at compile time

Super and Sub Classes

The extends clause specifies the super class of this class. The implements clause specifies the interfaces being implemented by this class. Member declarations can be declarations of fields, methods, and nested classes. The field, method, and nested class declarations can be intermixed. The order of declarations is immaterial to the compiler. Thus, they should be ordered in a way that is most logical and comprehensible.

Modifiers

  • Modifiers in Java can define accessibility and

can be applied to both classes and class

members (sub-classes, fields and methods).

  • Some of the modifier keywords are shared by

both classes and methods.

  • When using access modifiers only one can be

used at a time.

Scope

  • Scope
    • Part of program where a variable may be referenced
    • Determined by location of variable declaration
      • Boundary usually demarcated by { }
  • Example

public MyMethod1() { int myVar; myVar accessible in ... method between { } }

Scope – Example

package edu.umd.cs ;

public class MyClass1 {

public void MyMethod1() { ... } public void MyMethod2() { ... }

}

public class MyClass2 {

}

Method

Method

Class Package

Class

Scope

Other Modifiers

  • In addition to scope, modifiers can refer to

related concepts, such as limiting access to a

thread in a multithreaded environment

( synchronized ) or indicating that a

method or field belongs to the whole class

instead of a single instance ( static ).

Modifier Examples

  • public class MyApplet extends

java.applet.Applet{…}

  • private boolean killJabberwork;
  • static final double weeks = 9.5;
  • protected static final int MEANINGOFLIFE

= 42

  • public static void main(String

arguments[]{…}

Class Modifiers

  • Syntax for class modifiers:

[ClassModifier] class ClassName {…}

  • Example:

public class Employee {…}

Class Modifiers

  • Important points for class modifiers:
    • A single Java file can only contain one class that is declared public.
    • An abstract class cannot be instantiated.
    • A static nested class does not have an implicit reference to the enclosing class.

Method Modifiers

  • static - accesses only static fields.
  • final - may not be overridden in subclasses.
  • abstract - defers implementation to its subclasses.
  • synchronized - atomic in a multithreaded

environment

  • native - compiled to machine code by Assembler, C or

C++.

Method Modifiers

  • Syntax for method modifiers:

[MethodModifers] ReturnType MethodName ([ParameterList) {}

  • Example:

private void GetEmployee(int ID) { }