Tuesday, 13 March 2012

Java inheritance

Object-oriented programming allows classes to inherit properties and methods from other classes.
In the following example, "Truck" class inherits properties and methods from "Car" class.
This syntax is really useful because many classes are used for making a software and some classes can be modified easily by using this function. It's cool!
-----------------------------------------------------------------------
public class Truck extends Car
{
         int load;

 public Truck (String name, String type, String model, int load)
 {
          super(name,type,model);
          this.load=load;
 }

 public String toString()
 {
           return  super.toString()+"  The limit of load: " + load;
 }
-----------------------------------------------------------------------

No comments:

Post a Comment