Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Explain Overriding Methods: The Solution

The object oriented solution to this problem is to describe a new class, call it SlowCar, that inherits from Car and imposes the additional constraint in which a car may not go faster than 70 mph (112.65 kph).

To do this you'll required to adjust the two places that speed can be changed, the constructor and the accelerate() method. The constructor has a different name since all constructors are named after their classes other than the accelerate() method must be overridden. This means the subclass has techniques with the similar signature as the method in the superclass.
public class SlowCar extends Car {

private static final double speedLimit = 112.65408; // kph == 70 mph

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

super(licensePlate, 0, maxSpeed, make, model, year,
numberOfPassengers, numDoors);
this.accelerate(speed);

}

public void accelerate(double deltaV) {

double speed = this.getSpeed() + deltaV;

if (speed > speedLimit) {
super.accelerate(speedLimit - this.getSpeed());
}
else {
super.accelerate(deltaV);
}
}
}
The first thing to remember about this class is what it doesn't have, getSpeed(), getLicensePlate(), getMaximumSpeed(), setLicensePlate() methods or speed, maxSpeed and numDoors fields. All of these are given through the superclass Car. Nothing about them has modified so they don't required to be repeated here.

Further look at the accelerate() method. This is various than the accelerate() method in Car. It imposes the additional constraint.

The constructor is a little more complicated. First remember that if you're going to use a non-default constructor, which is a constructor with arguments, you do required to write a constructor for the subclass, even if it's just going to do the exact similar thing as the matching constructor in the superclass. You cannot easily inherit Car's constructor because in which constructor is named Car() and this one must be named SlowCar().

The constructor requires to set the value of name, url, and description. Therefore they're not accessible from the subclass. In the other words they are set through calling the superclass's constructor using the keyword super. While super is used as a method in the first non-blank line of a constructor, it stands for the constructor of this class's superclass.
The immediate superclass's constructor will be known as in the first non-blank line of the subclass's constructor. If you don't call it explicitly, then Java will call it for you along with no arguments. It's a compile time error if the immediate superclass doesn't have a constructor with no arguments and you don't call a various constructor in the first line of the subclass's constructor.

The use of the ternary operator in the constructor call is unusual. Therefore, it's essential to meet the compiler's requirement that the invocation of super be the first line in the subclass constructor. Or else this could be written more clearly using only if-else.

Java, Programming

  • Category:- Java
  • Reference No.:- M9547240

Have any Question?


Related Questions in Java

Assessment instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Assignment - method in our madnessthe emphasis for this

Assignment - "Method in our Madness" The emphasis for this assignment is methods with parameters. In preparation for this assignment, create a folder called Assign_3 for the DrJava projects for the assignment. A Cityscap ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As