Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Inheritance in java

Fri Feb 06, 2009 10:59 pm

Inheritance in java :
Inheritance is one of the main concepts of object oriented programming. The base class is named super (Base) class and the child class is named subclass. Using inheritance we can achieve the concepts of re-usability. The child class can use the methods and variables of the super class and add to them its own methods and variables. Inheritance represents problems in the real world.

Example on java inheritance :
java code
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:
  • Subclass can only inherit public and protected members but not private members.
  • 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 .

Read more about inheritance



Post a reply
  Related Posts  to : Inheritance in java
 What is Inheritance?!!     -  
 Inheritance C++ Example     -  
 Inheritance in c++     -  
 help me! inheritance     -  
 Need Help about Inheritance     -  
 inheritance in c++     -  
 @AttributeOverride with Inheritance     -  
 Multiple Inheritance     -  
 single table inheritance     -  
 Inheritance & polymorphism checker code     -  

Topic Tags

Java OOP