Explain overriding methods and the solution, JAVA Programming

Assignment Help:

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.


Related Discussions:- Explain overriding methods and the solution

Create a new project in eclipse , Task 1 Create a new project in Eclips...

Task 1 Create a new project in Eclipse called Assignment 1. Within this project create a package called task01. 1/ Download the class Date (you must use this class - no

Explain about drawing lines, Explain about Drawing Lines? Drawing strai...

Explain about Drawing Lines? Drawing straight lines along with Java is easy. Just call g.drawLine(x1, y1, x2, y2) where (x1, y1) and (x2, y2) are the endpoints of your lines

What happens to static fields of class during serialization, What happens t...

What happens to the static fields of a class during serialization? There are three exceptions in which serialization does not necessarily read and write to the stream. These

Write names of the dos attacks phases, Write names of the DoS attack's phas...

Write names of the DoS attack's phases? DoS (Denail of service): DoS attach has in total 3 kinds of phases and below they are listed: 1. Search 2. Arm 3. Attack

Explain difference between the boolean and operator, What is the difference...

What is the difference between the Boolean & operator and the && operator? If an expression including the Boolean & operator is evaluated, both operands are evaluated. Then the

Describe two reasons driving the termination of processes, Question 1: ...

Question 1: (a) Describe two reasons driving the termination of processes. (b) Discuss the five main steps the OS would implement once it has decided to create a new proc

Explain bean lifecycle in spring framework, 1. The spring container searche...

1. The spring container searches the bean's definition from the XML file and instantiates the bean. 2. Using the dependency injection, spring populates all of the properties as

What can an applet do, What Can an Applet Do? An applet can: • Draw ...

What Can an Applet Do? An applet can: • Draw pictures on a web page • Create a new window and draw in it. • Play sounds. • Receive input from the user by the keyboard or

What is the parent class for all swing components? , Design pattern : As y...

Design pattern : As you may look from the diagram given below, containers collect components. Sometimes you want to accumulate a container to another container. So, a container co

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