How To Get Access From Variables In Super Class Or Subclass
Dec 2, 2014
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;
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 ?
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 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.
I will like to add to the questions about constructors and its this. I have a class A with a constructor, i have a class B which initialize the constructor. also i have a class C which needs a variable in class A but doesn't need to initialize the constructor of A. the question how do i access the variable of class A without initializing the constructor.
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 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.
I'm writing a simple program in which I have a super class Person, inherited by the subclasses Customer and Employee (they inherit the variables ID, name and surname).
Java Code:
public class Person { int id; String name; String surname; public Person () {
[Code] .....
However the problem is here: when I try to get the variables ID, name and surname through my main class, they fail to return (0,null,null). Why is this? I have get-Methods in my subclasses which should return the super variables, but they are not.
Java Code:
public String getUser() { return username; }
public String getName() { return super.name; } mh_sh_highlight_all('java');
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.
So the first thing we did was make a program called memory calculator. now we are supposed to make a sub class that modifies the program to also do exponents and natural logs. Then to make a new class that acts as the driver for what is now called the scientific memory calculator which includes the exponent and log. I have watched this through debug and im a little confused. first off it creates a variable for currentValue in current and in sci which are my instances, which i dont think is what i really want to do, I am unsure how to get the things in the Scientificmemcalc to do stuff without calling them directly. so when i try to do the power feature it does everything correct but it sets the sci current value to the correct answer, and now i cant figure out how to pass it on to the current current value.
This is the memory calculator :
public class MemoryCalculator { public static void main(String[] args) { MemoryCalculator current = new MemoryCalculator();//creates an instance int choice = 0; do{//main loop System.out.println("Current value: " + current.getCurrentValue() + "
I created a superclass Ships and under that a class CivilShips. Under that HumanBulkFreighter.
I declared variables in Ships and CivilShips and wanted to have them set in HBF to a specific value. When I know try to compile them I get the following:
Does a variable have public access modifier? if we can use it within the class and outside of the class then can i access a public variable as follows??
class mo { void display() { public int a=9; System.out.println(a); } public static void main(String[] args) { mo m=new mo(); m.display(); } }
ERROR: It shows 6 errors :-O. Error 1. illegal start of the expression 2. class,interface, or enum expected
I just want to be able to read type and weight in the Letter class I created (I created read out messages to check in the Letter class). I am able to read it with in the Mail class.
package org.mailprice.postage; import java.util.Scanner; public class Mail { static Letter letter;
I have 2 classes. TestClassA has 1 getter and 1 setter. From my second class, TestClassB, I want to access the getter method of TestClassA. If I try to access it, I get null. How do I do it?I do not want the methods to be declared as static. How can the getter method value be printed in TestClassB (without marking anything as static)?
public class TestA { private String name; public String getName() { return name;
I've been unable to figure out how to access an objects data from another class. I ended up missing a lesson in java and haven't been able to catch up on this topic on my own through my textbook.
Error: has private access
Code:
public class TestCoffeeDrinker { public static void main(String[] args) { Coffee latte = new Coffee("Starbucks Tall Latte", 2.85); Coffee mocha = new Coffee("Starbucks Grande Mocha", 3.95); Coffee mcdonalds = new Coffee("McDonalds McCafe", 0.99); System.out.println(mcdonalds.toString());
why I cant access a variable from another class, in particular why in the main class cant i assign " quiz1 = getQuizOne();" ? It constantly giving me error.
Java Code:
import java.util.Scanner; public class Grade { private int quiz1, quiz2, midtermExam, finalExam = 0; public static void main(String[] args) { Student John = new Student(); John.StudentData();
I make a coding a subject system for my internship project, I use JComboBox to choose name of faculty. I want to call selected item of faculty from another class to choose subject under the selected faculty.
How can I call selected item from another class?
This is my coding for select faculty.
public class lectInterface(){ private void saveDataActionPerformed(java.awt.event.ActionEvent evt) { String staff_id=staffID.getText(); int staff_Id=Integer.parseInt(staff_id); String Name=staffName.getText();
[Code] ....
and below is coding for another class
try{ LectInterface lI = new LectInterface(); String host = "jdbc:derby://localhost:1527/STUDLECDB"; String uName = "studlecdb"; String uPass = "studlecdb"; Connection con = DriverManager.getConnection( host, uName, uPass );
I have a linked list of objects of a class I created called BaseballPlayer. I also have a Fielder and Pitcher class that extends BaseballPlayer. The linked list and list nodes accept elements of type BaseballPlayer. I put into the linked list both Fielder objects and Pitcher objects. The Pitcher class has a method that returns a float variable that is only found in the Pitcher class called EarnedRunAverage. However, in my main program it won't let me access the method. It says: The method getERavg() is undefined for the type BaseballPlayer. It does however, let me access methods in the super-class BaseballPlayer.
Here is main code and the Pitcher class code.
ListNode p = list.getFirst().next; while(p!=null){ if(p.player instanceof Fielder){ sortedFielders.append(Integer.toString(p.player.getNum()) +',');
Why can't I access a method from another class? For example, I want to get the value of get method from another class. It's giving me an error on if(getExamType() == 'M') That's what I've done, for example:
Java Code:
public static Exam[] collateExams(Exam[] exams){ Exam [] r = new Exam[50]; r = exams; Exam [] finalExam = new Exam[50]; for(int i = 0; i < r.length; i++) { if(getExamType() == 'M') { } } return r; mh_sh_highlight_all('java');
This time I have to make a Black Jack game ( I guess this is a classic) I have created Three classes for this BlackJack( Main), Card, and Player.
What I am trying to do is put the Give one card to the player and remove it from the deck into a separate procedure because I will be doing this several times during the game.
This is the code I have so far Under the class BlackJack.