196k views
3 votes
Create a class, under 15 lines of code, inheriting from Java API classes, with two constructors. The class should have no data items, and only two methods. Refer to the slides on exceptions for an example.

a) CodeSnippet.java

b) InheritedClass.java

c) ExceptionExample.java

d) ShortClass.java

User Caasjj
by
7.3k points

1 Answer

5 votes

Final answer:

The requested Java class is named ShortClass, extends the Exception class, and includes two constructors and two empty methods, meeting the specified requirements.

Step-by-step explanation:

The question asks for the creation of a class in Java that inherits from one of the Java API classes, has two constructors, does not store any data, and contains only two methods. The code below reflects these requirements and is an example of how this could be achieved, using Exception as a base class:

public class ShortClass extends Exception {
// Default constructor
public ShortClass() {
super();
}

// Constructor with a message
public ShortClass(String message) {
super(message);
}

// First method
public void methodOne() {
// Method implementation
}

// Second method
public void methodTwo() {
// Method implementation
}
}

This class does not declare any fields and provides two simple methods whose implementation would depend on the specific functionality desired.

User Teodron
by
7.8k points