I'm having difficulty understanding this small piece of code:
package food;
public abstract class Fruit {
private String color;
private String tasteType;
public void setColor(String color) {
this.color = color;
}
public abstract void setTasteType(String taste);
}
The above is an abstract class which describes the basic structure that every fruit should "extend".
The below is a concrete subclass of the Fruit class called Apple.
import food.Fruit;
class Apple extends Fruit {
public void setTasteType(String taste) {
tasteType = taste;
}
}
Also do note that the two pieces of code are in different packages!
Upon compiling the Apple class I get the following error:
Apple.java:4: error: tasteType has private access in Fruit
tasteType = taste;
^
What I don't understand is this: I've given a non-abstract implementation to the "setTasteType" method in the Apple class and clearly setTasteType should have the authority to modify the private instance variables of Fruit. But it turns out I'm wrong.
I just wanted to know that why are final and abstract called as modifiers ,what is the essence of calling them as modifiers since there are two types of modifiers access modifiers and non-access modifiers so final and abstract come under the second category ,so why are these called as modifiers?
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
interface: methods - abstract, default, static ONLY(abstract methods have no body, while static and defaults do, right?) fields - public, static, final ONLY abstract class: a normal class, but has at least one abstract method methods - all i.e., static, non-static, abstract (can it have a default method?) fields - all i.e., public, protected, private / final, non-final / static, non-static
why don't I define my methods in a class, rather than going a level up and declaring it first in an abstract class/interface? If the point is to have different implementations for different needs, then we have the option to override the methods.
I am writing small pieces of code to make sure I understand Java basics and I have the following.
package teams1; public abstract class Team1{ private String sport = new String(); public abstract String getSport(); public abstract void setSport(); } import teams1.*;
[Code] .....
It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1.
I am new to Java, and last week had an assignment to create a shopping list. I made it so that I have one class use a ProductData class to load an array of objects (description, price, priority). This week I need to take that program and change it so that it includes an Interface and Abstract Class. I need to also split one class up into at least 2 others.
I am having trouble getting my thoughts together and figuring out what to put in the interface and what to put in the abstract class. I'm thinking that it might be best to split up the ProductData class up into 3 different classes: description, price, and priority. Then have an interface with a print method. Each of those 3 classes will implement the interface.
As for the abstract class, have the price and priority extend the abstract class. The abstract class will be at the same level as the interface and contain the set and get methods. Right now they are of 2 different data types: int, double. Should I make both of them Double, and then use a method to change the priority to an int?
Should price and priority inherit from description, or should they all be at the same level? I am thinking that they should be at the same level because they all describe the item in the array.
My most confusing part is that I have no clue at all on how I can load that array when each object is split up in a different class. My professor went over ArayLists last week, and we can now use them if we want, but the assignment doesn't explicitly say that we should change it to an Array List. Where does the constructor for the ProductData() go? Do I split it up into 3 different constructors?
I am stuck. It seems like I have done everything by the book but I keep getting the same error: cannot find symbol. The error is specifically addressing lines 9, 10, and 11 in the Alien class file. All that is supposed to happen is an output of information for the two types of aliens.
Java Code:
import javax.swing.*; public class CreateAliens { public static void main (String[] args) { Martian aMartian = new Martian(); Jupiterian aJupiterian = new Jupiterian(); JOptionPane.showMessageDialog (null, "
I have a javafx class that has buttons and textfields. Here is the code for a button, and i want the button to make a new object but im having trouble setting the constructor
Label label = new Label("Type"); GridPane.setHalignment(label, HPos.RIGHT); TextField textField = new TextField();
Label label2 = new Label("First Name"); GridPane.setHalignment(label2, HPos.RIGHT); TextField textField2 = new TextField();
[Code] ....
after i create the object i will insert the object in an arraylist of person objects
I am currently building a Plugin system for an application and I wanted to add a little security feature.
I have read many tutorials on this and they basically all came down to the same answer:
"Its complicated: You need a custom class loader and security manager and ... and even then its still tricky"
So I thought, before I load the classes dynamically I will simply read the class files. Why bother with a SecurityManager or ClassLoader if I can simply whitelist all allowed classes and search for all illegal Class access before I even load anything.
My working directory is e:ajava. in package p1 i create two classes c1 and c2. net beans creates three files e:ajavap1srcp1, e:ajavap1srcc1, e:ajavap1srcc2. package runs without a hitch. i create another package p2 under e:ajava. i want to use class c1 in p2. pray what on earth should be my import statement in the p2 source code after the first statement 'package p2'. another related querry what should i include in my class path so as to gain access to c1 and c2 in source code of p2.
I know whats the interfaces and abstract class and also know that difference between interface and abstract class,but here my doubt is eventhough abstract class more advantage than the interface,then why should we use interfaces and when?
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
I passed my abstract class private final reference to another concrete class and I used abstract class reference as parameter to that concrete class constructor and in my main method and null to that parameter then only that program executes correctly...i placing my code below ..if there is any error tell me where is error occurring then i will check my code...i think my code is right but little bit doubt abstract class concept.
{ } class concept1 extends concept { private final concept parent; public concept1(concept aparent) { parent=aparent; System.out.println(parent); } public static void main(String args[]) { //concept p=new concept1(null); concept c=new concept1(null); }}