0
Creating Objects
public class Animal { void bark() { System.out.println("Woof-Woof"); } void miaw() { System.out.println("Miaw!"); } } class MyClass { public static void main(String[ ] args) { Animal dog = new Animal(); Animal cat = new Animal(); dog.bark(); cat.miaw(); } }
6 Antworten
+ 7
Owain , please use appropriate tags for the programming language when posting a question. Thanks!
+ 1
You can use one object to call bark() and miaw()
It is save memory of your code.
+ 1
Animal x=new Animal();
x.bark();
x.miaw();
0
How would that look?
0
Am I right in thinking that this would give the same output, but if I need "cat" and "dog" for later then my way is ok?
0
Sorry, it was meant to be under a lesson, where it would have been clear