What is inheritance in java explain with example, JAVA Programming

What is Inheritance in java Explain with example?

Code reusability is claimed to be a key advantage of object-oriented languages over non-object-oriented languages. Inheritance is the mechanism through that this is achieved. An object can inherit the variables and techniques of another object. It can remain those it wants, and replace those it doesn't need.

For instance, let us also expand the Car class so that a car also has a make, a model, a year, a number of passengers it can carry, four wheels, either two or four doors. That class might look like this:
public class Car {

private String licensePlate; // e.g. "New York A456 324"
private double speed; // kilometers per hour
private double maxSpeed; // kilometers per hour
private String make; // e.g. "Ford"
private String model; // e.g. "Taurus"
private int year; // e.g. 1997, 1998, 1999, 2000, 2001, etc.
private int numberPassengers; // e.g. 4
private int numberWheels = 4; // all cars have four wheels
private int numberDoors; // e.g. 4

// constructors
public Car(String licensePlate, double maxSpeed,
String make, String model, int year, int numberOfPassengers,
int numberOfDoors) {

this(licensePlate, 0.0, maxSpeed, make, model, year,
numberOfPassengers, numberOfDoors);

}

public Car(String licensePlate, double speed, double maxSpeed,
String make, String model, int year, int numberOfPassengers) {

this(licensePlate, speed, maxSpeed, make, model, year,
numberOfPassengers, 4);
}

public Car(String licensePlate, double speed, double maxSpeed,
String make, String model, int year, int numberOfPassengers,
int numberOfDoors) {

// I could add some more constraints like the
// number of doors being positive but I won't
// so that this example doesn't get too big.
this.licensePlate = licensePlate;
this.make = make;
this.model = model;
this.year = year;
this.numberPassengers = numberOfPassengers;
this.numberDoors = numberOfDoors;

if (maxSpeed >= 0.0) {
this.maxSpeed = maxSpeed;
}
else {
maxSpeed = 0.0;
}

if (speed < 0.0) {
speed = 0.0;
}

if (speed <= maxSpeed) {
this.speed = speed;
}
else {
this.speed = maxSpeed;
}
}

// getter (accessor) methods
public String getLicensePlate() {
return this.licensePlate;
}

public String getMake() {
return this.make;
}

public String getModel() {
return this.model;
}

public int getYear() {
return this.year;
}

public int getNumberOfPassengers() {
return this.numberPassengers;
}

public int getNumberOfWheels() {
return this.numberWheels;
}

public int getNumberOfDoors() {
return this.numberDoors;
}

public double getMaxSpeed() {
return this.maxSpeed;
}

public double getSpeed() {
return this.speed;
}

// setter method for the license plate property
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}

// accelerate to maximum speed
// put the pedal to the metal
public void floorIt() {
this.speed = this.maxSpeed;
}

public void accelerate(double deltaV) {

this.speed = this.speed + deltaV;
if (this.speed > this.maxSpeed) {
this.speed = this.maxSpeed;
}
if (this.speed < 0.0) {
this.speed = 0.0;
}
}
}
Obviously this doesn't exhaust everything there is to say about a car. Which properties you choose to involves in your class depends on your application. 

Posted Date: 5/15/2013 6:14:54 AM | Location : United States







Related Discussions:- What is inheritance in java explain with example, Assignment Help, Ask Question on What is inheritance in java explain with example, Get Answer, Expert's Help, What is inheritance in java explain with example Discussions

Write discussion on What is inheritance in java explain with example
Your posts are moderated
Related Questions
How to initialize array in java ? Individual elements of an array are referenced through the array name and through an integer that represents their position in the array. The

The Struts tag libraries are: ? HTML Tags ? Logic Tags ? Template Tags ? Bean Tags ? Tiles Tags ? Nested Tags

Authentication is basically an identification step. Functionality used for J2EE security: Principal : An entity that may be authenticated and identified. Principal n

Draw: loops through the list of cards drawing them in a single row starting from the screen position referred to by the 'x' and 'y' fields of the Hand class. You can limit the numb

Explain the difference between pre-emptive scheduling and time slicing ?

Describe Graphics Objects ? In Java all drawing takes place via a Graphics object. This is an example of the class java.awt.Graphics. Initially the Graphics object you use w

1. The purpose of this problem is to practice using ArrayLists.Write a Java client file containing a mainmethod plus other methods as needed to solve the following problem (no clas

This assignment is a good project for working with 2 dimensional arrays. In this program, you will implement an ascii version of the classic game "Connect 4". Connect 4 is a 2 p

What is PortableRemoteObject.narrow() method and what is used for?                Ans) Java RMI-IIOP gives a mechanism to narrow the the Object you have received from from your

The Action Class is type of the Model and is a wrapper around the business logic. The purpose of Action Class is to change the HttpServletRequest to the business logic. To use the