Firstly, Thanks everybody that read that topic.
How can if statement become true in test class? I couldnt find any solution.I couldnt write any code in these method.I tried to send from Room class numberOfTiger to class Question's method but I didnt achieve that.
That's question about ,How can I change int variable(numberofTiger) to Cat.Tiger variable.After that if statement become true to invoke (getNumberOfTiger) method.
public class Test {
public static void main(String[] args) {
Animal an = new Animal();
Animal.Cat an1 = an.new Cat();
Animal.Cat.Tiger an2 = an1.new Tiger(3, 900, 2);
if (Animal.Question.getnumberOfTiger(an2) == 3) {
System.out.println("True");
}
}
}
public class Animal {
Cat[] c;
// inner class
class Cat {
Tiger[] t;
// inner class
class Tiger {
private int numberOfTiger;
private int averageOfTigerWeigth;
private int youngTiger;
public Tiger(int numberOfTiger, int averageOfTigerWeigth, int youngTiger) {
super();
this.numberOfTiger = numberOfTiger;
this.averageOfTigerWeigth = averageOfTigerWeigth;
this.youngTiger = youngTiger;
}
static class Question {
static int getnumberOfTiger(Cat.Tiger a) {
return 0;
}
}
}
In addition to either making Cat a static class, or using its instance,
you also need a getter for a.numberOfTiger since it is private, in Tiger class:
public getNumberOfTiger() {
return numberOfTiger;
}
Then:
return a.getNumberOfTiger();
In getNumberOfTiger() you need to return the number of tigers associated with that object. You are currently just returning 0, so it will always evaluate to false.
I see the issue. The Tiger class and the Cat class needs to be static. The reason is, a non-static inner class can call on its outer class (e.g. Cat.this.something). A non-static inner type is called like this:
instanceOfOuterClass.innerClass
whereas a static inner type is called like this:
outerClassName.innerClass
The simplest way to call on a non-static inner type is new Outer().new Inner(); The main issue with beginners in Java is that they try to do this:
new (new Outer()).Inner()
But the actual way to call it is
new Outer().new Inner()
Also, your method is always returning 0 for the count of tigers.
Related
I want to define my property and function in anonymous class as under
ExistingExtendableJavaClass aClass = new ExistingExtendableJavaClass() {
public String someProperty;
public String getMyProperty() { return someProperty }
});
But then these calls don't work
aClass.someProperty // not accessible
aClass.getMyProperty() // not accessible
I know because ExistingExtendableJavaClass doesn't have these, but then my anonymous has these. How can I achieve this ?
They are accessible just fine:
new ExistingExtendable() {
public void foo() {}
}.foo();
works great.
But if you write:
ExistingExtendable x = new ExistingExtendable() {
public void foo() {}
};
x.foo();
That does not work. For the same reason this doesn't work:
Object o = new String();
o.toLowerCase(); // nope
The problem is that your anonymous class has no name, thus, you cannot denote its type. We can fix the string example by replacing Object o with String o, but there is no String equivalent.
However, that is the point of an anonymous inner class.
If you want these to be denotable, then you don't want an anonymous inner class. Asking: "I want an anonymous inner class, but I want the new members I declared in them to be accessible" is like asking: "I want a circle, but.. with corners".
You can make method local inner classes, and now you have names:
public void example(String x) {
class IAmAMethodLocalClass extends ExistingExtendableJavaClass {
String someProperty; // making them public is quite useless.
String foo() {
System.out.println(x); // you can access x here.
}
}
IAmAMethodLocalClass hello = new IAmAMethodLocalClass();
hello.someProperty = "It works!";
}
an anonymous inner class is the same as this method local class thing, except it avoids naming the type. In this case, you NEED that name, thus, you can't use the anonymous inner class construct.
I want to define my property and function in anonymous class as under
ExistingExtendableJavaClass aClass = new ExistingExtendableJavaClass() {
public String someProperty;
public String getMyProperty() { return someProperty }
});
But then these calls don't work
aClass.someProperty // not accessible
aClass.getMyProperty() // not accessible
I know because ExistingExtendableJavaClass doesn't have these, but then my anonymous has these. How can I achieve this ?
They are accessible just fine:
new ExistingExtendable() {
public void foo() {}
}.foo();
works great.
But if you write:
ExistingExtendable x = new ExistingExtendable() {
public void foo() {}
};
x.foo();
That does not work. For the same reason this doesn't work:
Object o = new String();
o.toLowerCase(); // nope
The problem is that your anonymous class has no name, thus, you cannot denote its type. We can fix the string example by replacing Object o with String o, but there is no String equivalent.
However, that is the point of an anonymous inner class.
If you want these to be denotable, then you don't want an anonymous inner class. Asking: "I want an anonymous inner class, but I want the new members I declared in them to be accessible" is like asking: "I want a circle, but.. with corners".
You can make method local inner classes, and now you have names:
public void example(String x) {
class IAmAMethodLocalClass extends ExistingExtendableJavaClass {
String someProperty; // making them public is quite useless.
String foo() {
System.out.println(x); // you can access x here.
}
}
IAmAMethodLocalClass hello = new IAmAMethodLocalClass();
hello.someProperty = "It works!";
}
an anonymous inner class is the same as this method local class thing, except it avoids naming the type. In this case, you NEED that name, thus, you can't use the anonymous inner class construct.
I am trying to learn java. Forgive me if my concepts are not clear or very wrong.
I am trying to create inheritance and polymorphism application.
I have created an array of Animals[5]. I am trying to add refrences of dog, cat to the array.
I want it to hold
Animals[0] = zooDog
I am getting error that
cannot make a static reference to the non-static
I have create AnimalstestDrivve class
package animals;
public class AnimalstestDrive {
public Animals[] myZoo = new Animals[5];
int zooCounter = 0;
public static void main(String[] args) {
//Set animals array
Dog zooDog = new Dog();
addAnimals(zooDog);
Cat zooCat = new Cat();
addAnimals(zooCat);
}
public void addAnimals(Animals a){
if ( zooCounter > 5 ){
myZoo[zooCounter] = a;
zooCounter++;
}
else
System.out.println("Zoo is full");
}
}
here is my Animals class
package animals;
public abstract class Animals {
private String Name;
private int Size; //Size on the scale 1 to 10
public void eatFood(){
System.out.println("I am eating food");
}
public void sleep(){
System.out.println("I am sleeping now");
}
abstract public void makeNoises();
}
Simple dog, cat class
package animals;
public class Dog extends Animals {
public void makeNoises(){
System.out.println("Bow! bow!");
}
}
The main method (static) attempts to call the addAnimals method, whose declaration is non-static. You need to create an instance of the class first, then call the method on this instance
AnimalstestDrive testDrive = new AnimalstestDrive();
Dog zooDog = new Dog();
testDrive.addAnimals(zooDog);
See Understanding Class Members for more information
You need to have an instance of the class AnimalstestDrive. Static means, that you don't need any instance of the class to use the class method, so if you would mark the addAnimals as static, You could use that method without creating an instance of AnimalstestDrive.
Because the method addAnimals is not static, you need to create an instance of AnimalstestDrive to use that function.
When a method is not static, it is specific to an instance of that class. For example:
AnimalstestDrive atd = new AnimalstestDrive();
atd.addAnimals(new Dog()); // this will add animals to "atd"
If a method is static, it is not specific to an instance of the class, but the class itself.
If you put this method in the class AnimalstestDrive:
public static void staticMethod() {
}
You could only access it with AnimalstestDrive.staticMethod(), not atd.staticMethod().
More info on static methods here.
This program is supposed to individually call the funFact of each subclass but instead, it calls the funFact method only from the Mammal class. What am I doing wrong?
public class MammalFacts{
public static class Mammal{
public static String funFact(){
return "If you are reading this, there's a 70% chance you're a mammal";
}//end funFact
}//end mammal
public static class Primate extends Mammal{
public static String funFact(){
return "All primates can fly";
}
}//end Primate
public static class Monkey extends Primate{
public static String funFact(){
return "Monkies will rule the earth someday";
}
}
public static void main(String[]args){
Mammal[]i = new Mammal[3];
i[0] = new Mammal();
i[1] = new Primate();
i[2] = new Monkey();
for(int c = 0; c < i.length; c++){
System.out.println(i[c].funFact());
}
}//end of main
}//end MammalFacts
funFact is static. Overriding doesn't work on static methods.
Remove the static keyword from all your methods (as you are calling them via an instance reference anyway) and it will work as you expected.
If you "override" the static method, you are hiding the method, not really overriding.
read this:
https://docs.oracle.com/javase/tutorial/java/IandI/override.html
the "Static Methods" section exactly answers your question.
I have a ArrayList<A> and am putting "B" objects into it. In the code for "B" I am overriding the print function. However, in my code I end up getting the print function defined in "A" instead.
I was wondering if there was a way to get the "B" print function without using a if statement checking if "A" is a "instanceof" "B", if true then casting "A" to "B", then calling print. Let's also say I can't make the class abstract and can't make print abstract.
Here is my code to show what I mean:
Class A
public class A {
public String print() {
System.out.print("A");
}
}
Class B
public class B extends A {
public String print() {
System.out.print("B");
}
}
Main:
ArrayList<A> objects = new ArrayList();
objects.add(new B());
Class a = objects.get(0);
a.print();
Output = "A"
So this will print "A". Again, I know I can use a if statement check if it's from the "B" class then cast type to object to it. But let's say I am going to put the whole alphabet into it and I don't want an if and a bunch of else if statements? Is that the only way?
This seems to work for me.
public class testing{
private static class A{
public void print(){
System.out.println("A");
}
}
private static class B extends A{
#Override
public void print(){
System.out.println("B");
}
}
public static void main(String[] args) {
ArrayList<A> objects = new ArrayList<A>();
objects.add(new B());
A a = objects.get(0);
a.print();
}
}
upon looking at your code, the print functions should return void not String, or better yet they should actually return a String instead of printing within the method (wither way this fixes compile errors).
You are missing the type argument when your create your objects ArrayList (seem my example).
Its better practice to add the #Override annotation to tell the compiler that you are in fact overriding a method (see example)
Lastly you cannot declare an instance of A the way you have (Class a = objects.get(0);) you need to actually declare it as type A or B (again see my example)