CSE/IT 213 - : New Mexico Tech
CSE/IT 213 - : New Mexico Tech
Overloading methods
Not the same as overriding a method
Methods in subclasses that have different signatures
(parameter types)
Can have different return types as well
Can change access modifier
Overriding Methods
Have to have the same signature
Have to have compatible return types
class Animal {
public Animal getSnack() {
....
}
}
class Dog extends Animal {
public Dog getSnack() {
...
}
}
2
This is okay because a sublcass can do anything a parent class can do.
Have to keep the access levels the same or less restrictive
Cant go from public to private, but can go from
private to public.
Usually keep access levels the same
Overriding leads to polymorphism
Polymorphism
Polymorphism allows the reference type and object type
to be different.
You can treat specialized objects as more general types
of objects while still taking advantage of their specialized behaviors.
Dynamic binding happens at runtime
Use @override annotation
class Animal
$ java Animal
The animals are sleeping
Pig is sleeping
Horse is sleeping
Dog is sleeping
class Animal
10
$ java Animal
The animals are sleeping
animals eat everything
Pig is sleeping
pigs eat slop
Horse is sleeping
horses love hay
Dog is sleeping
dog eat dog food
11
12
Abstract Classes
The above example is bad design
You really wont ever create a new Animal()
What is an Animal really?
Class Animal describes general characteristics that subclasses will inherit.
No need to implement specific methods for class Animal
Do this by declaring classes abstract, a YAM
13
class AnimalTest {
public static void main(String[] args) {
Animal [] animalFarm = new Animal[3];
animalFarm[0] = new Pig();
animalFarm[1] = new Horse();
animalFarm[2] = new Dog();
Food food = new Food();
for ( Animal a : animalFarm) {
a.sleep();
food.getFood(a);
}
}
14
}
abstract class Animal
Dog is sleeping
dog eat dog food
15
a.sleep();
food.getFood(a);
}
}
}
$ javac AnimalTest.java
AnimalTest.java:5: Animal is abstract; cannot be instantiated
Animal b = new Animal();
ArrayList Revisited
Every class extends Object
The mother of all classes think polymorphic!
ArrayList has a bunch of methods that return or has
parameters that are Objects
public Object clone()
Returns a shallow copy of this ArrayList instance.
public boolean remove(Object o)
Removes the first occurrence of the specified element
from this list, if it is present.
20
import java.util.*;
class AnimalTest {
public static void main(String[] args) {
ArrayList<Animal> animalFarm = new ArrayList<Animal>();
Pig pig = new Pig();
animalFarm.add(pig);
Horse horse = new Horse();
animalFarm.add(horse);
Dog dog = new Dog();
animalFarm.add(dog);
Food food = new Food();
21
import java.util.*;
class AnimalTest {
public static void main(String[] args) {
ArrayList<Object> animalFarm = new ArrayList<Object>();
Pig pig = new Pig();
animalFarm.add(pig);
Horse horse = new Horse();
animalFarm.add(horse);
for (int i = 0; i < animalFarm.size(); i++) {
System.out.println("getClass(): " +
24
animalFarm.get(i).getClass());
System.out.println("hashCode(): " +
animalFarm.get(i).hashCode());
System.out.println("toString(): " +
animalFarm.get(i).toString());
}
}
}
$ java AnimalTest
getClass(): class Pig
hashCode(): 870314914
toString(): Pig@33dff3a2
getClass(): class Horse
hashCode(): 871639881
toString(): Horse@33f42b49
Another example
Added new method to dog class bark().
import java.util.*;
class AnimalTest {
public static void main(String[] args) {
ArrayList<Animal> animalFarm = new ArrayList<Animal>();
Pig pig = new Pig();
animalFarm.add(pig);
Horse horse = new Horse();
animalFarm.add(horse);
Dog dog = new Dog();
animalFarm.add(dog);
for (int i = 0; i < animalFarm.size(); i++) {
if (animalFarm.get(i) instanceof Dog) {
animalFarm.get(i).bark();
}
}
}
}
$ javac AnimalTest.java
AnimalTest.java:17: cannot find symbol
symbol : method bark()
location: class Animal
animalFarm.get(i).bark();
26
Bottom Line
Can only call method if the class of the reference variable has that method.
28
Interfaces
What do you do if you want to use Dog in a new way?
If I want to use it in a Vet class.
Do I have to add abstract methods to the Animal class?
If so, I have to implement them and is every animal
going to see a vet?
If I put the new Vet methods only inside the classes that
need them, then I destroy polymorphism.
29
}
class BluRayPlayer extends Player() {
play();
}
What happens when you want to create an Universal
Player, one that plays CDs, DVDs, and Blu-Ray? What
play() method would you call if you inherited all three?
31
To define an interface
public interface <ClassName> { }
To implement an interface use the implements keyword
public class <ClassName> extends <ParentClass> implements
<100% Abstract Class> {}
For example
public class Dog extends Animal implements Vet {}
32
Interface syntax
For methods
just need the return type, the signature followed by a
semi-colon.
No need for public abstract implicitly defined for you
//delaration
interface Vet {
boolean isSick();
Date takeShot();
}
33
//implementation
class Dog extends Animal implements Vet {
public boolean isSick() {
....
}
public Data takeShot() {
....
}
}
34
35