CIS 121 February 2, 2000


  • We have discussed packages and how they are used. The rules associated with packages are
    1. When we create a named package

      1. The package statement must be the first executable line of our Java program.
      2. The package name must also be the name of the directory in which our program is stored, and this directory must be in the CLASSPATH.

    2. When we use a default package

      1. The classes in the default package must be in the CLASSPATH
      2. The Java program must not have a package statement in it.
  • Let us review the use of the dot operator.

    When we invoke methods, we may use the dot operator.

    <Part A>.<Part B>

    Part A may be either a class name or an object name. Part B may be either a variable name or a method name.

    Class names are distinguished from object names by the the first letter of their name. The first letter of a class name is capitalized, and the first letter of an object name is lower case.

    Examples:
    1. USA.getInt() - Class name and method
    2. PostOfficeBox.width - Class name and variable
    3. myPostOffice.displayPOBoxes() - Object name and method
    4. myPostOffice.lastInUse - Object name and variable
  • Recall the definition of a class:

    <Access Control Keyword> class <extends classa> <implements interfacea>

  • Access Control Keywords - recall that we can have four access control keywords - none, public, private, and protected. Now let us look at what what these keywords mean.
    Access Control Keyword Where is the item available
    public everywhere
    none In the same class and package
    private In the same class
    protected In the same class, package, and subclass


  • There are four ways in which we can obtain classes we need to solve problems in Java.
    1. Use a class from the JDK if it exists
    2. Buy a prewritten package
    3. Create your own class using inheritance
    4. Write your own
  • The Extends clause

    The Extends Clause allows us to use inheritance. When we define a class we can use the extend clause to create subclasses. For example, in the definition

    public class B extends A {
    
    There are different ways of stating the relationship between A and B.
    1. Class A is called the super class of class B.
    2. Class A is called the parent class of class B.
    3. Class B is said to inherit from class A.
    4. A is called the base class from which B is derived.
  • Even though we don't state it explicitly, we use inheritance whenever we create an object. In the JDK, there is a class called Object. This class is the base class of every class we define without an extends clause. In class definitions where we use the extends class, the Object class is at the top of the class hierarchy.

  • An example of inheritance is the use of toString().

  • We can override methods in classes we inherit, but we must use the same signature.

  • Consider these examples of inheritance.
    1. A.java
    2. B.java
    3. C.java
    4. Room3D.java
  • Polymorphism is the concept of method calls taking on different forms.

  • Concrete Methods are methods which have implementations. Concrete classes are classes in which all the methods are concrete.

  • Abstract Methods are methods which have no implementation. That is, they only have a signature. Abstract Classes are classes in which at least one of its methods is abstract. We can't create objects of Abstract Classes.