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(); } }

28th Dec 2020, 7:17 AM
Owain
Owain - avatar
6 Antworten
+ 7
Owain , please use appropriate tags for the programming language when posting a question. Thanks!
28th Dec 2020, 10:40 AM
Lothar
Lothar - avatar
+ 1
You can use one object to call bark() and miaw() It is save memory of your code.
28th Dec 2020, 7:26 AM
{H}asintha Ranaweera
{H}asintha Ranaweera - avatar
+ 1
Animal x=new Animal(); x.bark(); x.miaw();
28th Dec 2020, 7:35 AM
{H}asintha Ranaweera
{H}asintha Ranaweera - avatar
0
How would that look?
28th Dec 2020, 7:33 AM
Owain
Owain - avatar
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?
28th Dec 2020, 7:37 AM
Owain
Owain - avatar
0
Sorry, it was meant to be under a lesson, where it would have been clear
29th Dec 2020, 5:03 PM
Owain
Owain - avatar