Java cares about type. It wont let you do something bizarre and dangerous like stuff a Giraffe reference into a Rabbit variable - what happens when someone tires to ask the so-called rabbit to hop()? And it won’t let you but a floating point number into an integer variable, unless you acknowledge to the compiler that you know you might lose precision (like, everything after the decimal point).
The compiler can spot most common problems:
Rabbit roger = new Giraffe();
When you see a statement like: " an object of type X ", think of type and class as synonyms.
How do we create a custom object?
- There is actually no such thing as an object variable.
- There's only an object reference variable.
- An object refference variable holds bits that represent a way to access an object.
- It doesn't hold the object itself, but it holds something like a pointer. Or an address. Except, in Java we don't really know what is inside a reference variable. We do know that whatever it is, it represents one and only one object. And the JVM knows how to use the reference to get the object.
Think of a Dog reference variable as a Dog remote control. You use it to get the object to do something. (invoke methods/behaviors )
Dog snoopy = new Dog();
snoopy.bark();
- Objective:
- To create tests that ensure expected behavior of each class:
- Cat
- Dog
- AnimalFactory
- CatHouse
- DogHouse
- To create tests that ensure expected behavior of each class:
- Purpose:
- To establish familiarity with Test-Driven-Development (TDD) practices.
- Getting context:
- Click here to gain more familiarity with TDD-structured programming.
- Create tests for
void setName(String name)- ensure that when
.setNameis invoked on an instance ofCat, thenamefield is being set to the respective value.
- ensure that when
- Create tests for
setBirthDate(Date birthDate)- ensure that when
.setBirthDateis invoked on an instance ofCat, thebirthDatefield is being set to the respective value.
- ensure that when
- Create tests for
String speak()- ensure that when
.speakis invoked on an instance ofCat, the value"meow!"is returned.
- ensure that when
- Create tests for
void eat(Food food)- ensure that when
.eatis invoked on an instance ofCat, thenumberOfMealsEatenis increased by 1.
- ensure that when
- Create tests for
Integer getId()- ensure that when
.getIdis invoked on an instance ofCat, the respectiveidvalue is returned.
- ensure that when
- Create test to check Animal inheritance; google search
java instanceof keyword- ensure that a
Catis aninstanceofan Animal
- ensure that a
- Create test to check Mammal inheritance; google search
java instanceof keyword- ensure that a
Catis aninstanceofa Mammal
- ensure that a
- Create tests for
void setName(String name)- ensure that when
.setNameis invoked on an instance ofDog, thenamefield is being set to the respective value.
- ensure that when
- Create tests for
setBirthDate(Date birthDate)- ensure that when
.setBirthDateis invoked on an instance ofDog, thebirthDatefield is being set to the respective value.
- ensure that when
- Create tests for
String speak()- ensure that when
.speakis invoked on an instance ofDog, the value"bark!"is returned.
- ensure that when
- Create tests for
void eat(Food food)- ensure that when
.eatis invoked on an instance ofDog, thenumberOfMealsEatenis increased by 1.
- ensure that when
- Create tests for
Integer getId()- ensure that when
.getIdis invoked on an instance ofDog, the respectiveidvalue is returned.
- ensure that when
- Create test to check Animal inheritance; google search
java instanceof keyword- ensure that a
Dogis aninstanceofan Animal
- ensure that a
- Create test to check Mammal inheritance; google search
java instanceof keyword- ensure that a
Dogis aninstanceofan Mammal
- ensure that a
- Create Test for
Animal createDog(String name, Date birthDate)- ensure that when
.createDogis invoked onAnimalFactoryTestaDogis created with the respectivenameandbirthDatevalue.
- ensure that when
- Create Test for
Animal createCat(String name, Date birthDate)- ensure that when
.createCatis invoked onAnimalFactoryTestaDogis created with the respectivenameandbirthDatevalue.
- ensure that when
- Create tests for
void add(Cat cat)- ensure that when
.addis invoked on theCatHouse, a respectiveCatobject can be retrieved from the house.
- ensure that when
- Create tests for
void remove(Cat cat)- ensure that when
.removeis invoked on theCatHouse, a respectiveCatobject can no longer be retrieved from the house.
- ensure that when
- Create tests for
void remove(Integer id)- ensure that when
.removeis invoked on theCatHouse, aCatobject with the respectiveidcan no longer be retrieved from the house.
- ensure that when
- Create tests for
Cat getCatById(Integer id)- ensure that when
.getCatByIdis invoked on theCatHouse, aCatwith the respectiveidis returned.
- ensure that when
- Create tests for
Integer getNumberOfCats()- ensure that when
.getNumberOfCats()is invoked on theCatHouse, the respective number ofCatobjects is returned.
- ensure that when
- Create tests for
void add(Dog dog)- ensure that when
.addis invoked on theDogHouse, a respectiveDogobject can be retrieved from the house.
- ensure that when
- Create tests for
void remove(Integer id)- ensure that when
.removeis invoked on theDogHouse, a respectiveDogobject can no longer be retrieved from the house.
- ensure that when
- Create tests for
void remove(Dog dog)- ensure that when
.removeis invoked on theDogHouse, aDogobject with the respectiveidcan no longer be retrieved from the house.
- ensure that when
- Create tests for
Dog getDogById(Integer id)- ensure that when
.getCatByIdis invoked on theDogHouse, aDogwith the respectiveidis returned.
- ensure that when
- Create tests for
Integer getNumberOfDogs()- ensure that when
.getNumberOfDogs()is invoked on theDogHouse, the respective number ofDogobjects is returned.
- ensure that when








