2nd May 2022, 2:25 PM
subrat yadav
11 Antworten
+ 7
You cannot make class inside class instead of static class. make Animal class outside Program class or make Animal class static public class Program { static class Animal { public void makesound(){ System.out.println("AJ"); } public void makesound(String dog) { System.out.println (dog); } } public static void main(String[] args) { Animal abc = new Animal(); abc.makesound(); } }
2nd May 2022, 2:30 PM
A͢J
A͢J - avatar
+ 3
Additionallly, I would recommend you to remove the blank spaces: System.out.println(...) Instead of System. out .println (...)
2nd May 2022, 2:35 PM
Lisa
Lisa - avatar
+ 3
Its a special problem of an inner class. Define class Animal out of class Program.
2nd May 2022, 2:38 PM
Oma Falk
Oma Falk - avatar
+ 2
subrat yadav Please after writing code check again what you are doing? You have many mistakes 1 - extends class Animal should be extends Animal 2 - public not publ8c 3 - void not voud // Created by NonStop CODING // why it has no output // Created by subrat yadav class Animal{ void makesound(){ System.out.println("space"); } void makesound(String dog){ System.out.println(dog); } } class Cat extends Animal { public void makesound(){ System.out.println("meau meau"); } } public class Program { public static void main(String[] args) { Animal abc= new Animal(); abc.makesound("dog"); Cat xyz=new Cat(); xyz.makesound(); } }
2nd May 2022, 2:54 PM
A͢J
A͢J - avatar
+ 1
class Cat extends Animal { public void makesound(){ System.out.println("meau meau"); } } It's "class Cat extends Animal" (line 14) And it's "public void" (line 15). Check your spellings.
2nd May 2022, 2:52 PM
Lisa
Lisa - avatar
+ 1
Here is an easy example with only one class how overloading works: https://code.sololearn.com/cggcHjdL0HEE/?ref=app
2nd May 2022, 3:00 PM
Denise Roßberg
Denise Roßberg - avatar
0
https://code.sololearn.com/cUnH1FzmU6lQ/?ref=app
2nd May 2022, 2:47 PM
subrat yadav
0
I suggest you to read more about inner classes to really understand your problem: https://www.baeldung.com/java-nested-classes
2nd May 2022, 2:51 PM
Denise Roßberg
Denise Roßberg - avatar
0
class animal must be static.
4th May 2022, 2:12 AM
Ahmad Khalid Tawakoli
Ahmad Khalid Tawakoli - avatar
0
you can not call a non-static method / class inside a static method / class.
4th May 2022, 2:13 AM
Ahmad Khalid Tawakoli
Ahmad Khalid Tawakoli - avatar