Total members 11890 |It is currently Thu Apr 18, 2024 7:38 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com