23

In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Embed Size (px)

DESCRIPTION

Overriding a Method Overriding: If you declare a variable within a class, and use the same variable name within a method of the class, then the variable used inside the method takes precedence, or overrides, the first variable

Citation preview

Page 1: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten
Page 2: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

In this class, we will cover:Overriding a methodOverloading a methodConstructorsMutator and accessor methodsThe import statement and using

prewritten classesPackages and the protected access

modifierThe finalize() method

Page 3: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Overriding a MethodOverriding:If you declare a variable within a class,

and use the same variable name within a method of the class, then the variable used inside the method takes precedence, or overrides, the first variable

Page 4: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Overloading a MethodOverloading:Involves using one term to indicate

diverse meaningsWriting multiple methods with the same

name, but with different argumentsOverloading a Java method means you

write multiple methods with a shared name

This is polymorphism in action.

Page 5: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

ConstructorsConstructors are a special type of method.Used to create an instance of the class.

e.g. Employee e = new Employee( );This calls the Employee constructor.

Java automatically provides a constructor method when you create a class

Programmers can write their own constructor classes

Programmers can also write constructors that receive argumentsSuch arguments are often used for initialization

purposes when values of objects might vary

Page 6: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Overloading ConstructorsIf you create a class from which you

instantiate objects, Java automatically provides a constructor

But, if you create your own constructor, the automatically created constructor no longer exists

As with other methods, you can overload constructorsOverloading constructors provides a way to create

objects with or without initial arguments, as needed

Page 7: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Example ofOverloading a Constructorpublic class Employee

{ public Employee (String n, double a) { name = n; salary = a; } public Employee ( ) { name = “ “; salary = 0; }}

Page 8: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Mutator and Accessor MethodsOften referred to as get/set methods.Mutator methods modify fields in a class.

Example: public void setName (String n) { empName = n; }

Accessor methods retrieve fields in a classExample:

public String class getName () { return empName; }

Page 9: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Mutator and Accessor MethodsReturning mutatable objects in public accessor

methods breaks encapsulation!!!!

Even if the data element is private, outside classes can now modify it.

You should return immutable objects (Strings, ints, etc).

If you must return a reference to a mutatable object, you should clone it first.

See pg. 112 in book for example of this rogue code.

Page 10: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

The finalize() MethodInherited from the Object class.Called by the VM before an object is destroyed

and it’s memory is releasedUse it to release resources that might not

otherwise be released (e.g. files)Use it to record the fact that an object has been

destroyed

Page 11: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

The Import Statement and Using Pre-written ClassesThe creators of Java wrote nearly 500

classes For example:

System, Character, Boolean, Byte, Short, Integer, Long, Float, and Double are classes

These classes are stored in packages, or a library of classes, which is a folder that provides a convenient grouping for classes

Page 12: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

java.lang – The package that is implicitly imported into every Java program and contains fundamental classes, or basic classes

Fundamental classes include:System, Character, Boolean, Byte, Short,

Integer, Long, Float, and Double Optional classes – Must be explicitly

named

The Import Statement and Using Pre-written Classes

Page 13: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

The Import Statement and Using Pre-written Classes

To use any of the prewritten classes (other than java.lang):Import the classORImport the package which contains the class you are

usingTo import an entire package of classes use the wildcard

symbol - * For example:

import java.util.*; //imports all java.util classes import java.util.Vector; //imports the Vector class

Represents all the classes in a package

Page 14: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

PackagesCreating packages encourages others to reuse

software because it makes it convenient to import many related classes at once

Packages are used to:maintain the uniqueness of class names

Using Packages in your programs prevent class name scope conflicts if multiple classes of the same name are used.

e.g. both java.util and java.sql have a Date class, so if you are using both packages you need to reference with java.util.Date or java.sql.Date.

group classes to make them more easily accessible to your classes

reference classes in a particular scope What access modifier helps you limit access to packages?

Page 15: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Packages and theProtected Access Modifier

the protected access modifier:Provides you with an intermediate level of

security between public and private accessIs used to limit access to classes within the same

packageIf you create a protected data field or method, it

can be used: within its own class in any classes extended from that class or in classes in the same package but it cannot be used by “outside” classes

Page 16: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Putting Your Class in a Package

To include your class into a package, use the package statementThe package statement must appear outside the class

definitionThe package statement looks like this:

package <name of package>; example: package MC697;

class Person { ... }

Page 17: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Packages and Directory StructurePackages map to the directory structure.

Example:package com.MC697;public class Test { public static void main (String[] args) { System.out.println(“Testing packages”); }}

This package statement maps to the directory: <base directory>/com/MC697 where base directory is the directory you are going to compile and execute the class file from

Page 18: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

Compiling and ExecutingUsing PackagesWhen using packages you must compile and run

from the base directory.So, let’s say c:\temp is the base directory we

want to use. The file in the example should be saved to c:\temp\com\MC697\Test.java.

To compile:cd to c:\temp

javac com/MC697/Test.javaTo execute:

cd to c:\tempjava com.MC697.Test

Page 19: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

JAR FilesJar files are Java’s version of the zip file.

They group packages and class files together in a unit to make it easier to deploy.

Can be viewed using Winzip or similar utility.

Page 20: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

JavadocsJavadocs are documentation for class files.Javadoc is a utility built into the sdk to automatically

build documentation from the java files. e.g. javadoc VectorDemo.java

Remember the /** …. */ documentation symbols? These are used to denote documentation comments. Put these before a method or field to include comments about

these in the javadocs. Special tags can be used:

@author @version @param for methods

Page 21: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

21

Page 22: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

22

Page 23: In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten

23