Chapter 7 Methods and Encapsulation Flashcards

1
Q

Images

A

https://github.com/zero14c/OCP-Notes/blob/2cb97c8c72f0031567538aaedef9cfe80971a0a9/images/Chapter%207%20images.md

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Tables

A

https://github.com/zero14c/OCP-Notes/blob/061afc7e53406ab3361d605b2f7eceae1f66c708/1Z0-815/Chapter%207%20Methods%20and%20Encapsulation/Chapter%207%20tables.md

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Methods and Encapsulation

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Method declaration

A

This is called a method declaration, which specifies all the information needed to call the method. There are a lot of parts, and we’ll cover each one in more detail. Two of the parts—the method name and parameter list—are called the method signature.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Method declaration

A

https://github.com/zero14c/OCP-Notes/blob/2cb97c8c72f0031567538aaedef9cfe80971a0a9/images/Chapter%207%20images.md

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Parts of a method declaration

A
public final void nap(int minutes) throws InterruptedException {
// take a nap
}
  1. Access modifier, public, optional
  2. Optional specifier, final, optional
  3. Return type, void, required
  4. Method name, nap, required
  5. Parameter list, (int minutes), required, but can be empty parentheses
  6. Optional exception list, throws InterruptedException, optional
  7. Method body, {// take a nap}, required for concrete method but can be empty braces, ommited for abstract method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to call method?
~~~
public final void nap(int minutes) throws InterruptedException {
// take a nap
}
~~~

A

To call this method, just type its name, followed by a single int value in parentheses:
nap(10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

ACCESS MODIFIERS

A

Java offers four choices of access modifier:
1. private The private modifier means the method can be called *only* from within the same class.
2. Default (Package-Private) Access With default access, the method can be called *only* from classes in the same package. This one is tricky because there is no keyword for default access. You simply omit the access modifier.
3. protected The protected modifier means the method can be called only from classes in the same package or subclasses. You’ll learn about subclasses in Chapter 8, “Class Design.”
4. public The public modifier means the method can be called from any class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Note:
There’s a default keyword in Java. You saw it in the switch statement in Chapter 4, “Making Decisions,” and you’ll see it again in the Chapter 9, “Advanced Class Design,” when I discuss interfaces. It’s not used for access control.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
public void walk1() {}
default void walk2() {} // DOES NOT COMPILE
void public walk3() {} // DOES NOT COMPILE
void walk4() {}
A

The walk1() method is a valid declaration with public access.

The walk4() method is a valid declaration with default access.

The walk2() method doesn’t compile because default is not a valid access modifier.

The walk3() method doesn’t compile because the access modifier is specified after the return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

OPTIONAL SPECIFIERS

A

can have multiple optional specifiers in the same method (although not all combinations are legal).
When this happens, you can specify them in any order.
And since these specifiers are optional, you are allowed to not have any of them at all. This means you can have zero or more specifiers in a method declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

OPTIONAL SPECIFIERS

A
  1. static The static modifier is used for class methods and will be covered later in this chapter.
  2. abstract The abstract modifier is used when a method body is not provided. It will be covered in Chapter 9.
  3. final The final modifier is used when a method is not allowed to be overridden by a subclass. It will also be covered in Chapter 8.
  4. synchronized The synchronized modifier is used with multithreaded code. It is on the 1Z0-816 exam, but not the 1Z0-815 exam.
  5. native The native modifier is used when interacting with code written in another language such as** C++**. It is not on either OCP 11 exam.
  6. strictfp The strictfp modifier is used for making floating-point calculations portable. It is not on either OCP 11 exam.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {} // DOES NOT COMPILE
public void final walk6() {} // DOES NOT COMPILE
final public void walk7() {}
A
  1. The walk1() method is a valid declaration with no optional specifier. This is okay—it is optional after all.
  2. The walk2() method is a valid declaration, with final as the optional specifier.
  3. The walk3() and walk4() methods are valid declarations with both final and static as optional specifiers. The order of these two keywords doesn’t matter.
  4. The walk5() method doesn’t compile because modifier is not a valid optional specifier.
  5. The walk6() method doesn’t compile because the optional specifier is after the return type.
  6. The walk7() method does compile. Java allows the optional specifiers to appear before the access modifier. This is a weird case and not one you need to know for the exam. We are mentioning it so you don’t get confused when practicing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

RETURN TYPE

A

The return type might be an actual Java type such as String or int. If there is no return type, the void keyword is used.

Note:
Remember that a method must have a return type. If no value is returned, the return type is void. You cannot omit the return type.

return statement is required in the method body if return type is not void.

When checking return types, you also have to look inside the method body.

Methods with a return type other than void are **required to have a return statement inside the method body. **

This return statement must include the primitive or object to be returned.

Methods that have a return type of void are permitted to have a return statement with no value returned or omit the return statement entirely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
public void walk1() {}
public void walk2() { return; }
public String walk3() { return ""; }
public String walk4() {} // DOES NOT COMPILE
public walk5() {} // DOES NOT COMPILE
public String int walk6() { } // DOES NOT COMPILE
String walk7(int a) { if (a == 4) return ""; } // DOES NOT COMPILE
A
  1. Since the return type of the walk1() method is void, the **return statement is optional. **
  2. The walk2() method shows the optional return statement that correctly doesn’t return anything.
  3. The walk3() method is a valid declaration with a String return type and a return statement that returns a String.
  4. The walk4() method doesn’t compile because the return statement is missing.
  5. The walk5() method doesn’t compile because the return type is missing.
  6. The walk6() method doesn’t compile because it attempts to use two return types. You get **only one return type. **
  7. The walk7() method is a little tricky. There is a return statement, but it doesn’t always get run. If a is 6, the return statement doesn’t get executed. Since the String always needs to be returned, the compiler complains.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
int integer() {
    return 9;
}
int longMethod() {
    return 9L; // DOES NOT COMPILE
}
A

When returning a value, it needs to be assignable to the return type.

int integerExpanded() {
int temp = 9;
return temp;
}
int longExpanded() {
int temp = 9L; // DOES NOT COMPILE
return temp;
}

This shows more clearly why you can’t return a long primitive in a method that returns an int. You can’t stuff that long into an int variable, so you can’t return it directly either.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

METHOD NAME

A

Method names follow the same rules as we practiced with variable names

An identifier may only contain letters, numbers, $, or _.

Also, the first character is not allowed to be a number, and reserved words are not allowed.

Finally, the single underscore character is not allowed.

By convention, methods begin with a lowercase letter but are not required to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
public void walk1() {}
public void 2walk() {} // DOES NOT COMPILE
public walk3 void() {} // DOES NOT COMPILE
public void Walk_$() {}
public _() {} // DOES NOT COMPILE
public void() {} // DOES NOT COMPILE
A
  1. The walk1() method is a valid declaration with a traditional name.
  2. The 2walk() method doesn’t compile because identifiers are not allowed to begin with numbers.
  3. The walk3() method doesn’t compile because the method name is before the return type.
  4. The Walk_$() method is a valid declaration. While it certainly isn’t good practice to start a method name with a capital letter and end with punctuation, it is legal.
  5. The _ method is not allowed since it consists of a single underscore.
  6. The final line of code doesn’t compile because the method name is missing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

PARAMETER LIST

A

Although the parameter list is required, it doesn’t have to contain any parameters. This means you can just have an empty pair of parentheses after the method name as follows:
void nap(){}

If you do have multiple parameters, you separate them with a comma.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
public void walk1() {}
public void walk2 {} // DOES NOT COMPILE
public void walk3(int a) {}
public void walk4(int a; int b) {} // DOES NOT COMPILE
public void walk5(int a, int b) {}
A
  1. The walk1() method is a valid declaration without any parameters.
  2. The walk2() method doesn’t compile because it is missing the parentheses around the parameter list.
  3. The walk3() method is a valid declaration with one parameter.
  4. The walk4() method doesn’t compile because the parameters are separated by a semicolon rather than a comma. Semicolons are for separating statements, not for parameter lists.
  5. The walk5() method is a valid declaration with two parameters.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

OPTIONAL EXCEPTION LIST

A

In Java, code can indicate that something went wrong by throwing an exception.

Exception list is optional.

For example, InterruptedException is a type of Exception. You can list as many types of exceptions as you want in this clause separated by commas. Here’s an example:
~~~
public void zeroExceptions() {}
public void oneException() throws IllegalArgumentException {}
public void twoExceptions() throws IllegalArgumentException, InterruptedException {}
~~~
You might be wondering what methods do with these exceptions. The calling method can throw the same exceptions or handle them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

METHOD BODY

A

The final part of a method declaration is the method body (except for abstract methods and interfaces).

A method body is simply a code block. It has braces that contain zero or more Java statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
public void walk1() {}
public void walk2() // DOES NOT COMPILE
public void walk3(int a) { int name = 5; }
A
  1. The walk1() method is a valid declaration with an empty method body.
  2. The walk2() method doesn’t compile because it is missing the braces around the empty method body.
  3. The walk3() method is a valid declaration with one statement in the method body.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Working with Varargs

A

a method may use a varargs parameter (variable argument) as if it is an array. It is a little different than an array, though.

A varargs parameter must be the last element in a method’s parameter list.

This means you are allowed to have only one varargs parameter per method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q
public void walk1(int... nums) {}
public void walk2(int start, int... nums) {}
public void walk3(int... nums, int start) {} // DOES NOT COMPILE
public void walk4(int... start, int... nums) {} // DOES NOT COMPILE
A
  1. The walk1() method is a valid declaration with one varargs parameter.
  2. The walk2() method is a valid declaration with one int parameter and one varargs parameter.
  3. The walk3() and walk4() methods do not compile because they have a varargs parameter in a position that is not the last one.
26
Q

When calling a method with a varargs parameter, you have a choice.
You can pass in an array, or
you can list the elements of the array and let Java create it for you.
You can even omit the varargs values in the method call and Java will create an array of length zero for you.

A
27
Q
15: public static void walk(int start, int... nums) {
16:     System.out.println(nums.length);
17: }
18: public static void main(String[] args) {
19:     walk(1); // 0
20:     walk(1, 2); // 1
21:     walk(1, 2, 3); // 2
22:     walk(1, new int[] {4, 5}); // 2
23: }
A
  1. Line 19 passes 1 as start but nothing else. This means Java creates an array of length 0 for nums.
  2. Line 20 passes 1 as start and one more value. Java converts this one value to an array of length 1.
  3. Line 21 passes 1 as start and two more values. Java converts these two values to an array of length 2.
  4. Line 22 passes 1 as start and an array of length 2 directly as nums.
28
Q
walk(1, null); // throws a NullPointerException in walk()
A

Since null isn’t an int, Java treats it as an array reference that happens to be null. It just passes on the null array object to walk. Then the walk() method throws an exception because it tries to determine the length of null.

29
Q
16: public static void run(int... nums) {
17:     System.out.println(nums[1]);
18: }
19: public static void main(String[] args) {
20:     run(11, 22); // 22
21: }
Line
A

Line 20 calls a varargs method with two parameters. When the method gets called, it sees an array of size 2. Since indexes are 0 based, 22 is printed.

30
Q

Applying Access Modifiers

A

private: Only accessible within the same class
Default (package-private) access: private plus other classes in the same package
protected: Default access plus child classes
public: protected plus classes in the other packages

31
Q

PRIVATE ACCESS

A

Private access is easy. Only code in the same class can call private methods or access private fields.

32
Q
1: package pond.duck;
2: public class FatherDuck {
3:     private String noise = "quack";
4:     private void quack() {
5:         System.out.println(noise); // private access is ok
6:     }
7:     private void makeNoise() {
8:         quack(); // private access is ok
9:     } 
10:}
A

FatherDuck makes a call to private method quack() on line 8 and uses private instance variable noise on line 5.

33
Q
1: package pond.duck;
2: public class BadDuckling {
3:     public void makeNoise() {
4:         FatherDuck duck = new FatherDuck();
5:         duck.quack(); // DOES NOT COMPILE
6:         System.out.println(duck.noise); // DOES NOT COMPILE
7: } }
A

BadDuckling is trying to access an instance variable and a method it has no business touching. On line 5, it tries to access a private method in another class. On line 6, it tries to access a private instance variable in another class. Both generate compiler errors. Bad duckling!

34
Q

DEFAULT (PACKAGE-PRIVATE) ACCESS

A

When there is no access modifier, Java uses the default, which is package-private access. This means that the member is “private” to classes in the same package. In other words, only classes in the package may access it.

35
Q
package pond.duck;
public class MotherDuck {
    String noise = "quack";
    void quack() {
        System.out.println(noise); // default access is ok
    }
    private void makeNoise() {
        quack(); // default access is ok
    }
}
A

MotherDuck can refer to noise and call quack(). After all, members in the same class are certainly in the same package. The big difference is MotherDuck lets other classes in the same package access members (due to being package-private), whereas FatherDuck doesn’t (due to being private).

36
Q
package pond.duck;
public class GoodDuckling {
    public void makeNoise() {
        MotherDuck duck = new MotherDuck();
        duck.quack(); // default access
        System.out.println(duck.noise); // default access
    }
}
A

GoodDuckling succeeds in learning to quack() and make noise by copying its mother. Notice that all the classes covered so far are in the same package pond.duck. This allows default (package-private) access to work.

37
Q
package pond.swan;
import pond.duck.MotherDuck; // import another package
public class BadCygnet {
    public void makeNoise() {
        MotherDuck duck = new MotherDuck();
        duck.quack(); // DOES NOT COMPILE
        System.out.println(duck.noise); // DOES NOT COMPILE
    }
}
A

Oh no! MotherDuck only allows lessons to other ducks by restricting access to the pond.duck package. Poor little BadCygnet is in the pond.swan package, and the code doesn’t compile.
Remember that when there is no access modifier on a member, only classes in the same package can access the member.

38
Q

PROTECTED ACCESS

A

Protected access allows everything that default (package-private) access allows and more. The protected access modifier adds the ability to access members of a parent class.

39
Q
package pond.shore;

public class Bird {
    protected String text = "floating"; // protected access
    protected void floatInWater() { // protected access
        System.out.println(text);
    }
}
package pond.goose;
import pond.shore.Bird; // in a different package

public class Gosling extends Bird { // extends means create subclass
    public void swim() {
        floatInWater(); // calling protected member
        System.out.println(text); // accessing protected member
    }
}
A

Gosling extends the Bird class.
Extending means creating a subclass that has access to any protected or public members of the parent class.
Running this code prints floating twice: once from calling floatInWater(), and once from the print statement in swim(). Since Gosling is a subclass of Bird, it can access these members even though it is in a different package.
Remember that protected also gives us access to everything that default access does. This means that a class in the same package as Bird can access its protected members.

40
Q
package pond.shore;

public class Bird {
    protected String text = "floating"; // protected access
    protected void floatInWater() { // protected access
        System.out.println(text);
    }
}
package pond.shore; // same package as Bird

public class BirdWatcher {
    public void watchBird() {
        Bird bird = new Bird();
        bird.floatInWater(); // calling protected member
        System.out.println(bird.text); // accessing protected member
    }
}
A

Since Bird and BirdWatcher are in the same package, BirdWatcher can access members of the bird variable.

The definition of protected allows access to subclasses and classes in the same package.

41
Q
package pond.inland;
import pond.shore.Bird; // different package than Bird

public class BirdWatcherFromAfar {
    public void watchBird() {
        Bird bird = new Bird();
        bird.floatInWater(); // DOES NOT COMPILE
        System.out.println(bird.text); // DOES NOT COMPILE
    }
}
A

BirdWatcherFromAfar is not in the same package as Bird, and it doesn’t inherit from Bird. This means that it is not allowed to access protected members of Bird.

42
Q

Subclasses and classes in the same package are the only ones allowed to access protected members.

A
43
Q
1: package pond.swan;
2: import pond.shore.Bird; // in different package than Bird
3: public class Swan extends Bird { // but subclass of Bird
4: public void swim() {
5: floatInWater(); // subclass access to superclass
6: System.out.println(text); // subclass access to superclass
7: }
8: public void helpOtherSwanSwim() {
9: Swan other = new Swan();
10: other.floatInWater(); // subclass access to superclass
11: System.out.println(other.text); // subclass access
12: // to superclass
13: }
14: public void helpOtherBirdSwim() {
15: Bird other = new Bird();
16: other.floatInWater(); // DOES NOT COMPILE
17: System.out.println(other.text); // DOES NOT COMPILE
18: }
19: }
A

Take a deep breath. This is interesting. Swan is not in the same package as Bird but does extend it—which implies it has access to the protected members of Bird since it is a subclass. And it does. Lines 5 and 6 refer to protected members via inheriting them.

Lines 10 and 11 also successfully use protected members of Bird. This is allowed because these lines refer to a Swan object. Swan inherits from Bird, so this is okay. It is sort of a two-phase check. The Swan class is allowed to use protected members of Bird, and we are referring to a Swan object. Granted, it is a Swan object created on line 9 rather than an inherited one, but it is still a Swan object.

Lines 16 and 17 do not compile. Wait a minute. They are almost exactly the same as lines 10 and 11! There’s one key difference. This time a Bird reference is used rather than inheritance. It is created on line 15. Bird is in a different package, and this code isn’t inheriting from Bird, so it doesn’t get to use protected members. Say what now? We just got through saying repeatedly that Swan inherits from Bird. And it does. However, the variable reference isn’t a Swan. The code just happens to be in the Swan class.

44
Q

the protected rules apply under two scenarios:

A
  • A member is used without referring to a variable. This is the case on lines 5 and 6. In this case, we are taking advantage of inheritance and protected access is allowed.
  • A member is used through a variable. This is the case on lines 10, 11, 16, and 17. In this case, the rules for the reference type of the variable are what matter. If it is a subclass, protected access is allowed. This works for references to the same class or a subclass.
45
Q
package pond.goose;
import pond.shore.Bird;
public class Goose extends Bird {
public void helpGooseSwim() {
Goose other = new Goose();
other.floatInWater();
System.out.println(other.text);
}
public void helpOtherGooseSwim() {
Bird other = new Goose();
other.floatInWater(); // DOES NOT COMPILE
System.out.println(other.text); // DOES NOT COMPILE
}
}
A

The first method is fine. In fact, it is equivalent to the Swan example. Goose extends Bird. Since we are in the Goose subclass and referring to a Goose reference, it can access protected members. The second method is a problem. Although the object happens to be a Goose, it is stored in a Bird reference. We are not allowed to refer to members of the Bird class since we are not in the same package and the reference type of other is not a subclass of Goose.

46
Q
package pond.duck;
import pond.goose.Goose;
public class GooseWatcher {
public void watch() {
Goose goose = new Goose();
goose.floatInWater(); // DOES NOT COMPILE
}
}
A

This code doesn’t compile because we are not in the goose object. The floatInWater() method is declared in Bird. GooseWatcher is not in the same package as Bird, nor does it extend Bird. Goose extends Bird. That only lets Goose refer to floatInWater() and not callers of Goose.

47
Q

PUBLIC ACCESS

A

public means anyone can access the member from anywhere.

48
Q

Note:
The Java module system redefines “anywhere,” and it becomes possible to restrict access to public code. When given a code sample, you can assume it isn’t in a module unless explicitly stated otherwise.

A
49
Q
package pond.duck;
public class DuckTeacher {
public String name = "helpful"; // public access
public void swim() { // public access
System.out.println("swim");
}
}

package pond.goose;
import pond.duck.DuckTeacher;
public class LostDuckling {
public void swim() {
DuckTeacher teacher = new DuckTeacher();
teacher.swim(); // allowed
System.out.println("Thanks" + teacher.name); // allowed
}
}
A

LostDuckling is able to refer to swim() and name on DuckTeacher because they are public. The story has a happy ending. LostDuckling has learned to swim and can find its parents—all because DuckTeacher made members public.

50
Q

Applying the static Keyword

A

When the static keyword is applied to a variable, method, or class, it applies to the class rather than a specific instance of the class.
The static keyword can also be applied to import statements.

51
Q
A

static methods don’t require an instance of the class.

They are shared among all users of the class.

You can think of a static variable as being a member of the single class object that exists independently of any instances of that class.

52
Q
public class Koala {
public static int count = 0; // static variable
public static void main(String[] args) { // static method
System.out.println(count);
}
}

public class KoalaTester {
public static void main(String[] args) {
Koala.main(new String[0]); // call static method
}
}
A
53
Q

In addition to main() methods, static methods have two main purposes:

A
  • For utility or helper methods that don’t require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate an object just to call the method.
  • For state that is shared by all instances of a class, like a counter. All instances must share the same state. Methods that merely use that state should be static as well.
54
Q
5: Koala k = new Koala();
6: System.out.println(k.count); // k is a Koala
7: k = null;
8: System.out.println(k.count); // k is still a Koala
A

The compiler checks for the type of the reference and uses that instead of the object—which is sneaky of Java. This code is perfectly legal:

Believe it or not, this code outputs 0 twice. Line 6 sees that k is a Koala and count is a static variable, so it reads that static variable. Line 8 does the same thing. Java doesn’t care that k happens to be null. Since we are looking for a static, it doesn’t matter.

55
Q

ACCESSING A STATIC VARIABLE OR METHOD

A

You just put the class name before the method or variable and you are done.

ex:
~~~
System.out.println(Koala.count);
Koala.main(new String[0]);
~~~

56
Q
A
57
Q

Note:
Remember to look at the reference type for a variable when you see a static method or variable. The exam creators will try to trick you into thinking a NullPointerException is thrown because the variable happens to be null. Don’t be fooled!

A
58
Q
Koala.count = 4;
Koala koala1 = new Koala();
Koala koala2 = new Koala();
koala1.count = 6;
koala2.count = 5;
System.out.println(Koala.count);
A

We hope you answered 5. There is only one count variable since it is static. It is set to 4, then 6, and finally winds up as 5. All the Koala variables are just distractions.

59
Q

STATIC VS. INSTANCE

A

A static member cannot call an instance member without referencing an instance of the class. This shouldn’t be a surprise since static doesn’t require any instances of the class to even exist.

60
Q
public class Static {
    private String name = "Static class";
    public static void first() { }
    public static void second() { }
    public void third() { System.out.println(name); }
    public static void main(String args[]) {
        first();
        second();
        third(); // DOES NOT COMPILE
    }
}
A

The compiler will give you an error about making a static reference to a nonstatic method. If we fix this by adding static to third(), we create a new problem. Can you figure out what it is?

All this does is move the problem. Now, third() is referring to nonstatic name. Adding static to name as well would solve the problem. Another solution would have been to call third as an instance method—for example,
new Static().third();.

61
Q

A static method or instance method can call a static method because static methods don’t require an object to use. Only an instance method can call another instance method on the same class without using a reference variable, because instance methods do require an object. Similar logic applies for the instance and static variables.

A
62
Q
public class Giraffe {
    public void eat(Giraffe g) {}
    public void drink() {};
    public static void allGiraffeGoHome(Giraffe g) {}
    public static void allGiraffeComeOut() {}
}
A