James PK's Technical Journal

[ Home | Journal ]


Sun, 14 Sep 2014

Java - Basic Example - Inheritance, Overriding, Vectors, Constructors

I needed to recap my Java knowledge recently. Here are snippets of a very basic example I wrote re-familiarise myself with Java.

Inheritance: The extends keyword is used:
class Car extends MotorVehicle {
}
Constructors: Which constructor is used depends on the argument to the constructor:
	MotorVehicle (String mType){
		this.mType=mType;
	}
	
	MotorVehicle (){
	}
Overriding: overriding a method:
   	@Override 
        public String toString() {
        	return mType + " " + maxSpeed;

Collections: A basic example of using a vector to store objects and retrieve:
    Vector  mvs = new Vector(5);

    mvs.addElement(mv1);
    mvs.addElement(mv2);

    for (int i = 0;i < mvs.size();i++) {
          System.out.println(mvs.get(i));
   	 }

posted at: 00:00 | path: /java | permanent link to this entry


Made with Pyblosxom