What is inheritance in java explain with example, JAVA Programming

Assignment Help:

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. 


Related Discussions:- What is inheritance in java explain with example

Write a java console application, You are to write a Java console applicati...

You are to write a Java console application, following the Software Life Cycle model, which will read personnel records from a text file and store them in a data structure.  One li

Prepare android antivirus with locate device capability, Prepare Android An...

Prepare Android Antivirus with locate device capability Project Description: We need a developer that can develop an Android Antivirus similar to lookout Developer need to prov

What are the various struts tag libraries, The Struts tag libraries are: ...

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

A service locator, J2EE prepares use of the JNDI interface to access differ...

J2EE prepares use of the JNDI interface to access different resources like JMS, JDBC, EJB etc. The client finds up for these resources through the JNDI look-up. The JNDI look like

Code java and javascript in liferay, We need a serious programmer who will ...

We need a serious programmer who will code Java and Javascript in Liferay - open to bidding Project Description: Big Data project Prototype in Liferay. Big Data + User onl

How to returning multiple values from methods, How to Returning Multiple Va...

How to Returning Multiple Values From Methods ? It is not probable to return more than one value from techniques. You cannot, for example, return the licensePlate, speed and m

Javascript Issue, I am having trouble with the code below because I am tryi...

I am having trouble with the code below because I am trying to call the text field input type so that whenever someone enters a number above 10 it responds with the alert shown bel

What is a framework, A framework is made up of the set of classes which per...

A framework is made up of the set of classes which permit us to use a library in a best possible way for a particular requirement.

Javaserver faces navigation model, What is JavaServer Faces navigation mode...

What is JavaServer Faces navigation model? Ans) A mechanism for describing the sequence in which pages in a JavaServer Faces application are shown.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd