Java workshop #3
Object Oriented Programming
Recap

primitive types
Recap (2)
- if- else
- while
- do - while
- for
Control statements
Today
- Objects & Classes
- Methods
- What exactly is Object Oriented Programming?
- Inheritance
- Polymorphism
Classes and objects
- Classes are the building blocks of a java program
- Classes contain the definition of what a program should do
- An object is an instance of a class in the program
- You can have multiple objects of the same class
Classes and objects(2)

Classes and objects(3)

Classes and objects (4)

A class contains
- Class Name
- Fields/variables - used for storing data
- Methods (functions/procedures) - used for manipulating data
- The fields and methods of a class are called class members
Let's create a new project
- Set "Animals" as the project name
Create a new class

- Right click on src.com.base
- Select New > Java class
- Set the class name: Dog
Let's define the "Dog" class
public class Dog {
//fields
public String name;
public String owner;
//methods
public void setName(String newName){
name = newName;
}
public void setOwner(String newOwner){
owner = newOwner;
}
public String getName(){
return name;
}
public String getOwner(){
return owner;
}
public void makeNoise(){
System.out.println(name + ":wuff! wuff!");
}
}
Let's create a "Dog" object
public static void main(String[] args) {
Dog myDog = new Dog();
System.out.println("The dog is called: " + myDog.getName());
}
Add the following code in the main method:
- Pay close attention to what is being printed in the IDE
- What is the dog's name?
Remember?

This is how a primitive type is declared
Reference types
Dog myDog = new Dog();
- myDog is an object
- In Java specific language myDog is called an object reference variable
- An object reference variable is a way to access an object

Just think of myDog as a remote control to a Dog

What is the difference?

What is the difference?

vs

So what happens?
1. We declare a reference variable
2. We create a object of type Dog
3. We link the reference and the object
Dog myDog
new Dog();
Dog myDog = new Dog();
Constructors
Dog myDog = new Dog();
- Constructors are used to tell the program to create a new object of a specific class
- A constructor is a special method that belongs to the class
- The purpose of a constructor is to initialize fields
- All classes have a default constructor even if we don't explicitly define one
- A constructor has the same name as the class
A class can have multiple constructors
So let's set the dog's name!
Add the following code into the Dog class:
public class Dog {
//add only this code below, don't change the whole class
public Dog(String newName){
name = newName;
}
//rest of the code from previous exercise remains here
}
The dog still has no name
- Even if we have defined a new constructor, we are still calling the default one
Dog myDog = new Dog();
- So let's name our dog. Add the following code into the main method and run the program
Dog myDog = new Dog("fred");
System.out.println("The dog is called: " + myDog.getName());
The dog has no owner
- To do: create another constructor in order to set both the name of the dog and the owner
- Print the name of the dog and the name of the owner
Try to do this by yourself. The solution is on the next slide
The dog's owner
- Add the new constructor in the Dog class
public Dog(String newName, String newOwner){
name = newName;
owner = newOwner;
}
- And create a new dog with an owner and a name:
public static void main(String[] args) {
Dog myDog = new Dog("fred");
Dog dogWithOwner = new Dog("benny", "Michael");
System.out.println("The dog is called: " + myDog.getName() +
" and his owner is: " + myDog.getOwner());
System.out.println("The dog is called: " + dogWithOwner.getName() +
" and his owner is: " + dogWithOwner.getOwner());
}
Can a dog have a new owner?

Sorry, dog!
- Add the following code in the Dog class:
public void setOwner(String newOwner){
owner = newOwner;
}
- Modify the main method
public static void main(String[] args) {
Dog myDog = new Dog("benny", "Michael");
System.out.println("The dog is called: " + myDog.getName() +
" and its owner is: " + myDog.getOwner());
myDog.setOwner("Iris");
System.out.println("The dog is called: " + myDog.getName() +
" and its new owner is: " + myDog.getOwner());
}
Methods
- Classes describe what objects should do
- Methods are used to tell an object what to do
- When methods are called on different objects they can have different behaviour
Defining a method
public void setOwner(String newOwner){
//method body
}
A method has:
- an access modifier: public
- a return type: void
- a name: setOwner
- 0 or more arguments: String newOwner
- a body: always between {}
void - means the method does not return anything
A method that returns something
public String getOwner(){
return owner;
}
If you declare a method to return a value, you must return a value of the declared type!
Let's try it out
public String getOwner(){
return 27;
}
- What is your IDE saying?
Let's add a new class
- Add the class "Cat" with the following code:
public class Cat {
public String name;
public String owner;
public Cat(String newName){
name = newName;
}
public Cat(String newName, String newOwner){
name = newName;
owner = newOwner;
}
public void setOwner(String newOwner){
owner = newOwner;
}
public String getName(){
return name;
}
public String getOwner(){
return owner;
}
public void makeNoise(){
System.out.println(name + ": meow!, meouw!");
}
}
Let's have a cat as well
public static void main(String[] args) {
Dog myDog = new Dog("Benny", "Michael");
System.out.println(myDog.getOwner() + " has a dog called " + myDog.getName());
Cat myCat = new Cat("Jenny", "Michelle");
System.out.println(myCat.getOwner() + " has a cat called " + myCat.getName());
}
What do you notice?
public class Cat {
public String name;
public String owner;
public Cat(String newName){
name = newName;
}
public Cat(String newName, String newOwner){
name = newName;
owner = newOwner;
}
public void setOwner(String newOwner){
owner = newOwner;
}
public String getName(){
return name;
}
public String getOwner(){
return owner;
}
public void makeNoise(){
System.out.println(name +
": meow!, meouw!");
}
}
public class Dog {
public String name;
public String owner;
public Dog(String newName){
name = newName;
}
public Dog(String newName, String newOwner){
name = newName;
owner = newOwner;
}
public void setOwner(String newOwner){
owner = newOwner;
}
public String getName(){
return name;
}
public String getOwner(){
return owner;
}
public void makeNoise(){
System.out.println(name
+ ":wuff! wuff!");
}
}
What do you notice?
- There is a lot of identical code in both classes.
- So, do we really need to write it twice?
Riddle
It can be a dog or it can be a cat.
It's an Animal!
Adding the Animal class
- Create a new class and name it "Animal"
public class Animal {
public String name;
public String owner;
public void setOwner(String newOwner){
owner = newOwner;
}
public String getName(){
return name;
}
public String getOwner(){
return owner;
}
}
Remove duplicated code
- From the Dog class remove the code that we added in the Animal class
public class Dog extends Animal{
public Dog(String newName){
name = newName;
}
public Dog(String newName, String newOwner){
name = newName;
owner = newOwner;
}
public void makeNoise(){
System.out.println(name + ":wuff! wuff!");
}
}
Remove duplicated code(2)
- From the Cat class remove the code that we added in the Animal class
public class Cat extends Animal{
public Cat(String newName){
name = newName;
}
public Cat(String newName, String newOwner){
name = newName;
owner = newOwner;
}
public void makeNoise(){
System.out.println(name + ": meow!, meouw!");
}
}
Inheritance
- Cat inherits from Animal
- Dog inherits from Animal
- Animal is a superclass
- Dog and Cat are subclasses
public class Cat extends Animal{}
public class Dog extends Animal{}
Inheritance
- Cat inherits from Animal
- Dog inherits from Animal
- Animal is a superclass
- Dog and Cat are subclasses
public class Cat extends Animal{}
public class Dog extends Animal{}
Do all animals make the same noises?
- Add the following code in the Animal class
public void setName(String newName){
name = newName;
}
public void makeNoise(){
System.out.println("I am an unknown animal so I keep quiet");
}
Do all animals make the same noises?
- Modify your main method to look like this
public static void main(String[] args) {
Animal myDog = new Dog("Benny", "Michael");
System.out.println(myDog.getOwner() + " has a dog called " + myDog.getName());
myDog.makeNoise();
Animal myCat = new Cat("Jenny", "Michelle");
System.out.println(myCat.getOwner() + " has a cat called " + myCat.getName());
myCat.makeNoise();
Animal unknownAnimal = new Animal();
unknownAnimal.setName("Mickey");
unknownAnimal.makeNoise();
}
What does this actually mean?
- A Dog IS AN Animal
Animal myDog = new Dog("Benny", "Michael");
Animal myCat = new Cat("Jenny", "Michelle");
- A Cat IS AN Animal
- IS A defines Inheritance
- If ClassA IS A ClassB then ClassA inherits ClassB
But is any animal a dog?
- What does your IDE say?
Animal animal = new Dog("Benny", "Michael");
Composition
public class Animal {
public String name;
public String owner;
//....
}
- An animal HAS A name
- An animal HAS A owner
- HAS A defines Composition
- Composition: An object can contain another object
Exercises
Reading recommendation

Java Workshop #3
By andr33a
Java Workshop #3
Java language basics: objects & classes, data types, control statements
- 905