Creating a Class |
Same as in C++
class Classname {
....
}
|
Creating Methods inside a Class |
Same as in C++
class Student {
String lastname;
String firstname;
void printName() {
System.out.println("Student's Name: " + firstname + " " + lastname);
}
}
|
Create an Instance of a Class |
Here is an example of the previously defined class:
Student s = new Student();
Exercise |
Define a Subclass |
-
If a class is not defined as a subclass of another it is automatically
a subclass of the Java class called Object.
-
Java only allows for a class to have one superclass (single inheritance).
-
A subclass inherits all of its parent's (as well as its superclasses')
methods and variables. The subclass may overwrite (create its own version)
these methods. To access such an overwritten parent method you can type:
super.method_name(...);
Example:
class GradStudent extends Student {
....
}
Exercise |
Class Protection Types |
public
package protection (default)
More Details |
Components of a Class |
-
Variables
-
Constructors (optional)
-
Special Kind of method called when creating an instance of a class that
may perform a any variety of operations including initializing variables,
creating other objects, etc.
-
Always have the same name as the class.
-
Can have multiple constructors that take different inputs and can perform
different operations.
-
If you do not define a constructor for a class, the default constructor
is invoked that does nothing.
-
Has NO return type.
-
If you write a class with a constructor with parameters and you intend
to possibly use it as a Paretn class you need to make sure you also write
a constructor with NO paramters. This is needed because: If the child class
is defined with no concstructors, it will automatically call super()
which is the Parent's constructor with No arguments.
-
Finalize Method (optional)
-
Called just before the object is garbage-collected and its memory reclaimed.
-
Must be defined as follows:
protected void finalize() throws Throwable { ...}
The default finalize() does nothing.
Methods
class Car {
/*VARIABLES*/
String model;
String manufacturer;
float Engine_Size;
/*CONSTRUCTORS*/
Car(String m, String man) {
model = m;
manufacturer = man;
}
Car(String m, String man, float e) {
model = m;
manufacturer = man;
Engine_Size = e;
}
/*METHODS*/
void PrintInfo() {
System.out.println("Model:" + model);
System.out.println("Make: " + make);
System.out.println("EngineSize: " + Engine_Size);
}
}
|
Interface Class |
-
See your book for more details.
-
It is the way that a set of method names, without definitions. By this
I mean the method's interface is presented but not implemented. This will
allow similar behavior to be duplicated acrros different parts of the class
hierarchy which is achievable in C++ through multiple inheritance which
Java does not allow.
-
Can not declare an object of this class....must create a new class that
extends this and implements all of the unimplemented methods.
-
Can not contain the main() method.
|
Abstract Class |
-
See book for more details.
-
Unlike an Interface Class, some methods may be implemented and the others
only the interface is declared. To declare an interface method you must
preceed it with the special keyword abstract as shown in the following
example:
abstract void PrintInfo();
Can not declare objects of this class...must create a new class that extends
this and implements all of the unimplemented methods.
Declare class and each method inside that is not implemented as abstract
by preceeding it with the term abstract
Can not contain the main() method.
Exercise |
Package |
Details |
Keywords |
-
super Refers to the parent class. Example: super.mmm() calls
the method named mmm in the parent class.
-
this Refers to the object itself. Example: this.mmm() calls
the method named mmm in the object itself.
|
Final |
-
Finalizing a class means that it can not be used to create subclasses.
-
Declare by preceeding the definition with the word final
|
Another option is to "get the Name" of the object in questions class.
This is done as follows: