Java: About Classes

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 
    • See here for details. 
  • 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 

IMPORTANT All applets are subclasses of the class Applet which is part of the package java.applet.

public class HelloAgainApplet extends java.applet.Applet{
}



Determining the Class of an Object

Below is an example of the instanceof operator that takes on the left the object and on the right the Class. It will return true if the object is an instance of this Class.
if (S1 instanceof String)
{  System.out.println("It is a String!"); }
 

 

Another option is to "get the Name" of the object in questions class. This is done as follows:

String name = obj.getClass().getName();
Exercise


Casting Objects

You are familiar with the idea of casting primitive types. You can do the same thing with objects as long as the class of the object your casting and the class you are casting to are related in terms of inheritance (one is the ancestor of the other or vica versa).

Example:

(Journal) MyPaper
here we cast the object MyPaper that is an instance of the class Paper to the class Journal where Journal is a subclass of Paper.


Comparing Objects

You can compare if two objects refer to the same object in memory using the == and != signs.
if(Object1 == Object2)
 { System.out.println("This is the same instance!"); }
Exercise