Objects and Classes

 

Class methods
Class variables
Enheritance
Interfaces
Example 1 - Classes and methods
Example 2 - Enheritance

 

Class methods

This example shows how to call a class method. From the main method, the add_two method is called, using input parameter 
input_number. The add_two class add's 2 to input_number and returns the result in output_number. Note that the class is 
declared as int, as it returns an integer, 
If you don't want to return a value, use void instead of the datatype in the decalration of the method
(public static void..). If you want to call a method of another class, prefix the call with the class 
name ( n = myClass.AddTwo(5) ) 
Note that all arguments to methods in jazva rare passed by value, and not by reference an dtherefore cannot be changed. 
This does not apply to arrays which can be changed.
public class FirstSample {
 public static int add_two(int input_number)
 { int output_number;   
   output_number = input_number + 2;
   return output_number;
 }

  public static void main(String[] args)
  {
     int n;
     n = add_two(5);
     System.out.println(n);
  }

}

Class variables

Class varables can be accesed by all the methods in the class. Note that unlike local varables are given default values automatically.
Class variables are are shared between instances.

public class Employee
{ private static double myNumber = 8.0;
  public static void main(String[] args)
  {.....
  }                                   

Enheritance

Preventing enheritance

If you want to prevent enheritance from a class, declare it as final.

final class myClass

Casting

If you convert an object reference from one class to another class, this is called casting.

The syntax for casting is the same as when you casts between variables.

The target type is surrounded with parantheses and placed before the object reference you want to cast:

Manager boss = (Manager)staff(0)

Reason to cast: To use an object in its full capacity after its actual type has been downplayed.

Abstract classes

Sometimes the topmost superclass will be so abstract that it is not used directly. To indicate that a method cannot be specified in this class, the abstract keyword is used.Abstract methods act as placeholder methods that are implemented in the subclasses.

public abstract class myClass
{
public abstract void play();
}

Protected

Methods and data can be protected. If they are protected, they can only be accessed by the subclasses of the class.

Interfaces

In java a class can't have more than one superclass. If you want to be sure that it implements certain methods that is not in its superclass, you can use interfaces. When using an interface, the class has to implement the methods in the interface.

You cannot put isnatnce fields or static methods in an interface, but you can supply constants in it.

Classes can implement more than one interface:

class Dog extends Animal implements Movements, Behaviour

Tthe example below consists of a super class Animal, an interface MoveMe and a subclass Dog that is an subclass of Animal and uses the interface MoveMe.

public class Animal
{

  public Animal() {
  }
  public void Talk()
  {
    System.out.println("Grrr");
  }
}
public class Dog extends Animal implements MoveMe
{  public void Dog()
   {
   }
   public void Jump()
   {System.out.println("I'm jumping like hell");
   }
   public void Run()
   {System.out.println("I'm running");
   }

}
public interface MoveMe
{//You can declare cosnatnts in interfaces
  public static final int SPEED_LIMIT = 25;
 //2 interface methods
  public void Jump();
  public void Run();

}
public class TestInterface {
  public static void main(String[] args)
  { Dog dog1 = new Dog();
    dog1.Talk();
    dog1.Jump();
    dog1.Run();
//  Writing a constant defined in the interface
    System.out.println(dog1.SPEED_LIMIT);
  }
}
Result:
Grrr
I'm jumping like hell
I'm running
25

 

Example 1 - Classes and methods

This simple example shows how to make an Animal class containing a class variable, a constructor and overloading method, an accessor method, a mutator method and normal method. The Animal test class declres two Animal objects, the one of them uses the overlay constructor. Thereafter the class shows how to use the animal class.

import java.util.*;

public class Animal
{
  // This variable retain its value for all isntances of the class
  private static int noOfInstances;

  public Animal(String i_name, int i_age)
  //This is an constructor class. A constructor class always have the same name as
  //the class. A constructor class is always called with the NEW keyword
  {
     name = i_name;
     age = i_age;
     //Count number of isntances
     noOfInstances++;
  }

  public Animal()
  //Example of overloading. The method has the same name as the preceeding method, but
  //different parameters
  {
     name = "Unknown";
     age = 0;
     noOfInstances++;
  }

  public int get_noOfInstances()
  //This is an "Accessor method" that returns the current value
  //of the field name
  //Note: Should allways start with get
  { return noOfInstances;
  }

  public void setAge(int new_age)
  //This is a "Mutator method"
  //Note: Should allways start with set
  {
   age = new_age;
  }

  public void Print()
  {
    System.out.println("Navn: " + name + " Alder: " + age);
  }
   //This variable is private to the Animal class
   private String name;
   private int age;
}

public class AnimalTest {

  public static void main(String[] args)
  {
//  Declare and initialize the class
    Animal animal1 = new Animal("Fido",2);
    Animal animal2 = new Animal(); //here we calls an overloading constructor
//  Call the Print method of the class
    animal1.Print();
    animal2.Print();
//  Call the accessor method get_noOfInstances,. This method returns
//  the number of instances of the Animal class
    int no_animals = animal1.get_noOfInstances();
    System.out.println("Antal dyr: " + no_animals);
//  Call the mutator method letAge that changes the age of an animal
    animal2.setAge(8);
    animal2.Print();
  }
}

 

Example 2 - Enheritance

This exapmle shows how a Bird class can enherit from its Animal super class

public class Animal {

  public Animal(String i_name) 
  { //Cosntructor
    name = i_name;
  }
  public void Speek()
  { //Note that this method is implemented differently
    //in the Bird class
    System.out.println("A generic animal says: Grrrrrrrrr");
  }
  public void Sleep()
  { //This method is the same for all subclasses
    System.out.println("All animals snores: ZZZZZZZZZZ");
  }

  public void writeName()
  {
    System.out.println("My name is: " + name);
  }
  private String name;

public class Bird extends Animal
{
  public Bird(String i_birdname)
  {//The bird constructor calls the cunstructor method of the
   //super class. The sub class constructot must always call
   //the super class constructor. 
    super(i_birdname);
  }

  public void Speek()
  {//New implementation of the Super class method Speek
    System.out.println("A bird says: Pip pip");
  }
}
public class testAnimal {
  public static void main(String[] args)
  {//A generic animal
    Animal generic_animal = new Animal("Generic animal");
    generic_animal.Speek();
   //A bird
    Bird bird1 = new Bird("Piphans");
    bird1.Speek();
    bird1.writeName();
    bird1.Sleep();


  }
}

Result:

A generic animal says: Grrrrrrrrr
A bird says: Pip pip
My name is: Piphans
All animals snores: ZZZZZZZZZZ

 

 

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)