Creating Objects | Sololearn: Learn to code for FREE!

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

12/28/2020 7:17:34 AM

Owain

6 Answers

New Answer

+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