COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

Each object has its own copy of all the instance variables of the class. ... Sometimes, you want to know how many objects from a particular class exist ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 18
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 14-Feb-08
  • Lecture Set 10
  • Static Class Members, Final Modifier, Inheritance

2
Static Class Members
  • Each object has its own copy of all the instance
    variables of the class.
  • Sometimes, only a single copy of a particular
    variable should be shared by all objects created
    from a class.
  • A static field (or class variable) is used in
    this case.
  • Represents classwise information
  • all objects in the class share the same piece of
    information

3
Keyword static
  • Used to declare a static variable
  • When to use static?
  • Sometimes, you want to know how many objects from
    a particular class exist
  • You can create a static count variable in the
    class definition --- and have that variable
    incremented/decremented upon object
    initialization/deinitialization.

4
Static
  • Static variables have class scope
  • Static class members exist even when no objects
    of the class exist
  • They are available as soon as the class is loaded
    into memory at execution time.
  • To access a public static member when no objects
    exist, prefix the class name and a dot (.) to the
    to the static member (such as Math.PI).
  • To access private static members when no object
    exists, there must be a public static method
    provided and the method must be called by
    qualifying its name with the class name and a dot.

5
Static Example Code
  • See Employee.java and EmployeeTest.java

6
Static Methods
  • A method declared static cannot acces non-static
    class members because a static method can be
    called even when no objects of the class have
    been instantiated.
  • Additionally, the this reference can not be
    used in a static method (same reason as above).

7
Static import
  • Enables you to refer to imported static members
    as if they were declared in the class that uses
    them.
  • This makes the class name and dot no longer
    required.
  • Syntax static import packageName.Classname.stati
    cMemberName

8
The final modifier
  • Final is the same as the old constant modifier
    in C, Pascal, etc
  • A class or a member of a class (variable, method,
    etc) can be declared final.
  • Once a value is set for this variable, it can not
    be changed in the program.

9
The final modifier
  • Final is used to help protect data integrity.
  • Example
  • final int universalAnswer 42
  • You can create a blank final variable
  • Final variable that is not initialized when it is
    created.
  • You can assign a value to the variable later, but
    it can not be changed once it is assigned

10
Blank final variable
  • //Create the variable, but dont initialize it
  • Final String name
  • //Give the variable a value that can not be
    changed
  • name new String(This is a string)

11
Inheritance
  • Technique that allows one class to be derived
    from another.
  • New class is created by absorbing an existing
    classs members and adding new or modified
    capabilities.

12
Superclasses and Subclasses
  • Superclass existing class
  • Called a base class in C
  • Also known as a parent class
  • Subclass new class
  • Inherits from the superclass
  • Called a derived class in C
  • Also known as a child class
  • Subclasses can become superclasses for future
    subclasses.

13
Subclasses
  • Usually more specific that the super class
  • Adds its own methods and fields
  • Exhibits the behaviors of the superclass, but
    with additional behaviors specific to the
    subclass.
  • Sometimes referred to as specialization.

14
Direct and Indirect superclasses
  • Direct superclass from which the subclass
    directly inherits
  • Indirect any class above the direct superclass
    in the class hierarchy.
  • Every class in Java directly or indirectly
    extends from the class Object.

15
Single vs Multiple Inheritance
  • Singe class is derived from one direct
    superclass
  • Multiple class is derived from more than one
    direct superclass
  • Not supported in Java (but supported in C)
  • Note Lecture on interfaces (later in the
    course) will explain how we can still obtain the
    idea of multiple inheritance in Java.

16
The keyword super()
  • When you create a child class, the child class
    constructor caninvoke the base class constructor
  • Do this with the keyword super -- acts as the
    call to the superclass constructor
  • The call to super() must be the first line of the
    derived class constructor
  • If explicit call to parent constructor not made,
    the subclass' constructor will automatically
    invoke super(). (the default constructor of the
    superclass, if there is one)
  • Can also use super to invoke a method from the
    parent class (from inside the derived class).
  • Super.method(parameters)

17
Inheritance Example
  • Recall the SuperString class you were asked to
    write for Assignment 3
  • See MegaSuperString.java and MegaSuperStringTest.
    java for example of inheritance
  • MegaSuperString inherits from SuperString
Write a Comment
User Comments (0)
About PowerShow.com