FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   A class and it's cousins
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: A class and it's cousins  (Read 370 times)
st33d

WWW Email
A class and it's cousins
« on: Aug 19th, 2004, 1:46am »

I had a discussion with my Dad about object code seeing as I'm getting to grips with the basics and he mentioned an ability of java to have a sort of template class from which I can make other slightly different classes of the same type.
 
Say I have an object class of cars. Now I want to make an object class of trucks. Same attributes and methods as the car but it's going to be slightly different with some new functions or attributes.
 
We looked it up and it was some kind of command called "extends". Now I've lost where it was in the book and I can't find it in this Java In a Nutshell book in front of me. I've Googled and searched but no joy. If some one could tell me the right name of this operation as well that would be handy too. It's not an inner class I'm talking about.
 
My friend is making a tile game in processing and this would help him greatly. With this approach he could set up his tile class and make the other tiles that have whatever strange properties as extensions. I'm mostly concerned whether there's a Processing way of doing it or whether you can just pop the Java right in there. My thanks if you can help.
 

I could murder a pint.
fjen

WWW
Re: A class and it's cousins
« Reply #1 on: Aug 19th, 2004, 8:22am »

Code:
void setup(){size(100,100);}
 
void draw(){
  cTemplate a = new cTemplate();
  extension1 b = new extension1();
  a.setS("A");
  b.setS("B");
  a.draw();
  b.draw();
  println(a.s);
  println(b.s); // extension1 inherited String s and setS from cTemplate ... //
}
 
class cTemplate
{   String s;
    void setS (String _s) { this.s = _s; }
    void draw () {line(0,0,20,20);}
}
 
class extension1 extends cTemplate
{    
    void draw () {line(20,20,40,20);}
}

in the example extension1 inherited the setS() method from cTemplate, as well as the variable s. the method draw() has been "overridden", means extension1 has it own version of it.
 
here is a tutorial on extends and inheritance:
http://java.sun.com/docs/books/tutorial/java/javaOO/subclasses.html
 
st33d

WWW Email
Re: A class and it's cousins
« Reply #2 on: Sep 22nd, 2004, 5:16pm »

Thank you very much. Exactly what I wanted to know.
 

I could murder a pint.
JohnG

WWW
Re: A class and it's cousins
« Reply #3 on: Sep 24th, 2004, 4:34pm »

You can use Interfaces as templates to, so you do something along the lines of:
 
public interface vehicle
{
   public string name;
   public string getname();
}
 
class car implements vehicle
{
  name="Car";
  string getname()
  {
    return name;
  }
}
 
Or something like that.. it's been a long time since I've done interfaces. This way you can't actually create a "vehicle" you have to specificly create a car, or a truck, etc, but you can still write methods that take a vehicle as a parameter, and have it work for the interface defined functions. I think.
 
Pages: 1 

« Previous topic | Next topic »