How To Get Superclass To Acknowledge Both Of Subclass Methods
Mar 14, 2015
I am trying to display the getCommands() method from my subclasses but I do not know how to cast them both. At the moment I can only display one animals getCommands() method.
public class Test {
public static void main(String[] args) {
Pet [] pet = new Pet[5];
pet[0] = new Dog("Scamp", 1, "run");
pet[1] = new Dog("Molly", 2, "fetch");
pet[2] = new Dog("Rover", 3, "dig");
So far I thought that setting superclass member variables as protected would allow the subclasses to access them using this. and that this was a good approach. However now after further reading am finding that actually these variables are better set as private and then accessed by the subclasses using public method (getters and setters) or constructor.
So my question is do you recommend setting them as private instead of protected and what would be the best way to access these variables from the subclasses ?
I am creating a set of 3 subclasses, 1 superclass, and an application. In my instructions it says to make set methods in my super and subclass by using dialog boxes. In the application you have 3 different arrays where you create objects and are supposed to call the methods from the subclasses to be used in the application. I don't know how to make the dialog boxes from my subclasses to show up in my application.
My understanding was I could override a method from the superclass, including with different parameters, but when I try to use super. it gives me an error the arguments have to match the superclass. But, if I do that it won't make any sense.
The first code below is the superclass. The issue I'm having is on the second code at lines 7 and 10. The ultimate goal is to make a new class where I'm able to display various packages with or without insurance.
public class Package { double shippingWeight; public char shippingMethod; final char air = 'A'; final char truck = 'T'; final char mail = 'M'; double shippingCost;
I would like to pass a variable I have in my main to my JFrame. In my main I calculate which pictures I should show in my JFrame. Is it possible to make objects of these pictures in my main class and use them in my JFrame? something like my code below.
public class JFrameTesting { public static void main(String[] args) { Image ImageVariable = Toolkit.getDefaultToolkit().getImage("C:1.jpg"); MyJFrame f = new MyJFrame(); f.setTitle("Drawing Graphics in Frames");
@MappedSuperclass public abstract class BssStandardEntityLaravel extends BssStandardEntity implements InterfacciaBssStandardEntity, Cloneable{ private static final long serialVersionUID = 1L; @Column(name = "created_at", nullable=true) @Temporal(TemporalType.TIMESTAMP) protected Date created_at = new Date();
[Code] ....
When i try to read some data with a JPA controller, this error is raised:
Persistent class "com.bss.libbssabstract.database.entity.BssStandardEntityLaravel" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class. org.datanucleus.store.rdbms.exceptions.NoTableManagedException: Persistent class "com.bss.libbssabstract.database.entity.BssStandardEntityLaravel" has no table in the database, but the operation requires it. Please check the specification of the MetaData for this class. at org.datanucleus.store.rdbms.RDBMSStoreManager.getDatastoreClass(RDBMSStoreManager.java:702)
[Code] ....
It requires BssStandardEntityLaravel table like a normal entity. I used the same entity package in other applications and it works perfectly. Why this error is raised?
I have a problem where I want to give each subclass of a certain class an index number (I don't care what index numbers are given, as long as there is a one-to-one relationship between subclasses and index numbers and the index numbers don't skip). This number will be used to sort the subclasses as an intermediate step to what I want to achieve. I know I could do this:
interface Superclass { int index(); } class Subclass implements Superclass { int index() { return 0; //or 1, or 2, ... } }
But this quickly gets tedious when I'm looking at lots of subclasses. Plus, there's the off chance that I could mess up and assign an index twice to two different subclasses by accident. Is there a better way to do this? I read about Annotations.
I am trying to prepare for the next installment Java course. I found a syllabus online from last year. All I'm trying to say is that I am not in this course but will be shortly. I tried the first project but I am having subclass issues. I want to access the getStock method in the Executive subclass from the client. I keep getting a cannot find symbol: method getStock from class Employee. I don't know why won't access Executive.
Main:
import java.util.*; public class EmployeeClient extends Employee { public static void main(String[] args) { Scanner input = new Scanner(System.in); //variables String name = " "; int totalSalary = 0; int stock = 0;
I'm having a difficult time running this piece of my program. I can't run the method findSmallest() in subclass NEW because I receive an error that says I have to declare the variable "smallest" as final, but then I won't be able to continue my code because "smallest" when I happen to use "smallest" again, it will always be set to 0.
package FindYourCourseGrades; import java.util.Scanner; public class FindYourGrades { public static void main (String[] args){ int number = 0; int counter = 0; int sum = 0; double average = 0; double smallest = 0;
There are a few things lacking in the TableView's keyboard navigation handlers. In tracing the code, the behavior is handled via TableViewBehavior and its super classes. If I want to augment that behavior, how do I do it?
Ideally, I would like to subclass TableViewBehavior, but I don't see how I can do it. This gets created in the TableViewSkin ctor:
public TableViewSkin(final TableView<T> tableView) { super(tableView, new TableViewBehavior<T>(tableView)); ... }
but as you can see there is no factory method to create the behavior class. If there was, I could subclass TableViewSkin and override the factory method.
I have a class called Sprite which extends its several subclasses. Therefore, there are a lot of different Sprite classes, the thing is however, most of those subclasses have unique types of variables which I want to only be included in those particular subclasses, not anywhere else. For instance, I might have a variable measuring distance in one subclass, and in another subclass there might be a height variable inherent. I don't want the first subclass to have both variables, neither the second or the main class. Because before I initialize my subclasses, I need to create the constructors of those subclasses in the main Sprite class first because it doesn't have the unique variables which those classes consist of. How do I prevent that? Now I have to create the unique constructors and variables for every subclass, when I only want them in their associated classes.
The super keyword when used explicitly in a subclass constructor must be the first statement but what about if i have a this(parameters) statements ? As the this one must also be the first statement... Does this means that i can have only one or the other ? What about when the super constructor is not explicit (aka implicit ) , can i use the this( parameters) in the same constructor ?
I'm working on a program design for a multimedia application its really just a learning process for myself about exploring application development, however there is a slight hiccup in the class inheritance hierarchy I think, and I'm not really sure why.
The problem being I cant set the constructor of subclass AnimatIntervalKeyFrame to be structured the same way as of the constructor of super class AnimatKeyFrame
This is the error given of the constructor of the subclass
public AnimatIntervalKeyFrame(int id, String category, Text text, ImageView image, int x, int y, int width, int height){ required: int,String,Text,ImageView,int,int,int,int found: no arguments
[Code] ....
package multimediasoftware.appComponent; import javafx.scene.image.ImageView; import javafx.scene.text.Text; // notes // class is declared abstract public abstract class AppComponent { // variables
how to get access from variables in a super class or a subclass. Here is what I got:
1) I have a super class that is in Jar file, I created a link in Eclipse, I know that the link is created correctly, I am going to concentrate just in one variable, so I don’t have to put all the code here firstName; in the super class(the one that is define in the path)
public class CommissionEmployee { // Field descriptor #6 Ljava/lang/String; private java.lang.String firstName;
I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass?
I have studied about the hierarchy of exception classes in which Throwable class comes at the top and two of its direct subclasses are Error and Exception..I just want to ask if in some code snippet we throw an instance of Error or its subclass within the try catch block then will that be also called "exception handling" ? I am asking this because Error class is not a child class of Exception therefore cant be said an Exception, therefore handling the same should not be called exception handling
I have written two methods called "contains" and "overlaps". The method "contains" is to detemine whether a point (x, y coordinate) is within the surface area of a square object. The location of the square objects is determined by the location of the upper left corner of the square.The method "overlaps" is to determine whether two square objects overlap each other.
I have written these as two separate methods. However I want to change the method "overlaps", so that it uses the method "contains" within it. I.e. using a method within a method. Thereby hopefully making the "overlaps" method a bit more clear and easy to read.
Java Code:
/** Returns true of the point with the coordinates x,y, are within the square. */ public boolean contains(int x, int y){ int sx = location.getX(); //"location" refers to a square object int sy = location.getY(); // getX() and getY() are to find it's coordinates // "side" is the side length of the square if (x >= sx && x <= (sx + side) && y >= sy && y <= (sy + side)){
Below is the main class of a project ive been working on, the goal is to start a countdown specified by the user. When the countdown reaches zero the base drops in the song that is being played. (Its not done yet) The main problem that arises is the fact that my song plays, and AFTER that, the timer starts.
Output:
Please input countdown in HH:mm:ss format. 00:00:41 Start? Yes
The name of of the song is: Skrillex & Damian "Jr Gong" Marley - "Make It Bun Dem"
The time of base drop is: 00:00:41 //Song starts here
//Song is done //Then timer starts 00:00:41 00:00:40 00:00:39
I have following code. In this code CSClient is an interface. All methods of CSClient are implementaed in CSClientImpl class. Do I not need CS Client Impl imported in this code ?
How can I call getBranch() of CSClient, which is not implemented in CSClient as " this. getCsClient(). get Branch (new CSVPath(vpath), true);" ? This code works fine without any error in eclipse.
How can a method getBranch(), which is implemented in CSClientImpl class be used in this code without importing CSClientImpl ?
Write the header for a method named send that has one parameter of type String, and does not return a value.Write the header for a method named average that has two parameters, both of type int, and returns an int value.
I have had to create a text analyser. I have created the program but it is all within the main method. The specification states that I have to have at least two methods within my Program.
import java.io.*; import java.text.SimpleDateFormat; import java.util.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class Analyser { public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
I'm working on a side project, which will eventually hopefully be a Pokedex, and I've just been going to it at the end of every chapter and using the stuff I've learned to work on it.So I just read chapter 3, which is all about variables and teaches how to use arrays.
my question is, does an array have to be declared inside a method? Because I'm trying to create an array inside a class without any methods and without the main, and I continuously get errors. Here's a quick working of my code that won't compile.
class blah { blah a[] = new blah[7]; a[0] = new blah(); }
The error message focuses on a[0] = new blah(); Telling me the 0 should be a ], the = is an illegal start of type, so on and so forth. The program compiles completely fine if it's within a method, like this:
class blah { void a() { blah a[] = new blah[7]; a[0] = new blah();
}
}
Or if I have public static void main (String[]args); But I'm trying to practice working outside of main.So does an array have to be within a method,