Joined: Tue Mar 27, 2007 10:55 pm Posts: 2279 Location: Earth Has thanked: 39 time Have thanks: 61 time
Inheritance in java : Inheritance is one of the main concepts of object oriented programming. The base class is named superclass and the child class is named subclass. Using inheritance we can achieve the concepts of reusability. The child class can use the methods and variables of the superclass and add to them its own methods and variables. Inheritance represents problems in the real world.
Example on java inheritance :
Code:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package codemiles; public class Car { protected int CarSpeed; protected String CarModel; private int ID=0;
public void setCarModel(String CarModel) { this.CarModel = CarModel; }
public void setCarSpeed(int CarSpeed) { this.CarSpeed = CarSpeed; }
public String getCarModel() { return CarModel; }
public int getCarSpeed() { return CarSpeed; } // Main Function public static void main(String[] args) {
}
} class RaceCar extends Car { public void RunTurbo() { CarSpeed+=100; }
} Any instance of of RaceCar can use the members of Car class except the variable (int ID) because it is private.
Notes: 1- Subclass can only inherit public and protected members but not private members. 2- You can only inherit from single class. (Single inheritance).That is means there is no multiple inheritance in Java. But as a solution to the multiple inheritance java supported class types called “interfacesâ€.
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )