kids encyclopedia robot

Class (programming) facts for kids

Kids Encyclopedia Facts

Class and object are basic building blocks in object-oriented programming languages. A class is written by a programmer in a defined structure to create an object (computer science) in an object oriented programming language. It defines a set of properties and methods that are common to all objects of one type.

For example, a class could be a car, which could have a color field, four tire fields, and a drive method. Another related class could be a truck, which would have similar fields, but not be exactly the same as a car. Both a car and a truck could be a kind of a third class which could be called a vehicle class. In this way, the programmer could create the parts of the program that are the same for both the car and the truck in programming the vehicle class, yet the programmer would be free to program how a car is different from a truck without duplicating all of the programming.

In this example, there are three classes: 1) a class called "car", 2) a class called "pick-up truck", and 3) a class called "vehicle". These classes would be used to make object. Although there is only one class called "car", there could be many objects that are created from the class called "car". And, although there is only one class that is called "truck", many objects of type truck could be created from this class. The class, vehicle, is actually general and there would probably not be any objects that were only of the class "vehicle". But a car would be a kind of vehicle and a truck would also be a kind of vehicle. So, you could say that both cars and trucks are from the same class, vehicle.

Inside of classes are two kinds of things: methods and fields (or properties). Methods can store the code for doing actions; in this example you could have a drive method and a brake method, and maybe a turnRight and turnLeft method. Fields store data; you could have a color field, a speed field and a size field. You can call methods by first making an object of the Car or Truck class, and call it for example ferrari, and doing ferrari.methodName(). In this case, if we wanted to make the vehicle move, we could do ferrari.drive().

Example

Here is an example realized with Java. Please notice that all unnecessary aspects are left out.

public class vehicle { // base class for all vehicles
  String color; // field color
  int position; // field position
  
  public void drive(int distance) { // method drive
    this.position += distance;
  }
}

public class car extends vehicle {
  int persons; // a new field calles persons
  
  public void getIn() { // method to get into the car
    this.persons++;
  }
  
  public void getOut() { // method to get out of the car
    this.persons--;
  }
}

public class truck extends vehicle {
  String load; // a new field called load
  
  public void load(String load) { // method to load the truck
    this.load = load;
  }
  
  public String unload() { // method to unload the truck
    String oldLoad = this.load;
    this.load = null;
    return oldLoad;
  }
}

Images for kids

See also

Kids robot.svg In Spanish: Clase (informática) para niños

kids search engine
Class (programming) Facts for Kids. Kiddle Encyclopedia.