Animal Class
public class Animal {
int age;
String gender;
int weightInLbs;
public Animal(int age, String gender, int weightInLbs) {
this.age = age;
this.gender = gender;
this.weightInLbs = weightInLbs;
}
public void eat() {
System.out.println("Eating...");
}
public void sleep() {
System.out.println("Sleeping...");
}
}
Zoo Class
public class Zoo {
public static void main(String[] args) {
Animal animal1 = new Animal(4, "F", 5);
animal1.eat();
Bird bird1 = new Bird();
Fish fish1 = new Fish("me luspa", 4);
}
}
Bird Class
public class Bird extends Animal {
public Bird(int age, String gender, int weightInLbs) {
super(age, gender, weightInLbs);
}
public void fly() {
System.out.println("Flying...");
}
}
Fish Class
public class Fish {
String tipi;
int mosha;
public Fish(String tipi, int mosha) {
this.tipi = tipi;
this.mosha = mosha;
}
public void swim() {
System.out.println("Swimming...");
}
}
Methods
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.
Java Class Attributes
In the previous chapter, we used the term "variable" for x in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class: