Creating Objects | Sololearn: Learn to code for FREE!


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


6 Answers

New Answer


Owain , please use appropriate tags for the programming language when posting a question. Thanks!


You can use one object to call bark() and miaw() It is save memory of your code.


Animal x=new Animal(); x.bark(); x.miaw();


How would that look?


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?


Sorry, it was meant to be under a lesson, where it would have been clear