(OOP) Chapter 3
(OOP) Chapter 3
} fleas = f;
} return fleas;
} }
public void speak() {
return System.out.println("Woof");
}}
Cont..
//Cat Subclass
public class Cat extends Animal {
private int hairballs;
public Cat(String n, int h) {
super(n); // calls Animal constructor
hairballs = h;
}
public int getHairballs() {
return hairballs;
}
public void speak() {
return System.out.println("Meow");
}
}
Cont..
Inheritance Quiz 1
What is the output of the following?
Dog d = new Dog("Rover" 3);
Cat c = new Cat("Kitty", 2);
System.out.println (d.getName() + " has " +d.getFleas() + " fleas");
System.out.println (c.getName() + " has " +c.getHairballs() + " hairballs");
//Outputs
============
Rover has 3 fleas
Kitty has 2 hairballs
//If I have this Food class: public class Beef extends Food {
private double weight;
public Beef(double w) {
public class Food {
weight = w
boolean raw; 1
}
boolean raw;
//is equivalent to:
public Food() {
public class Beef extends Food {
raw = true;
private double weight;
}}}}
public Beef(double w) { 2
super();
weight = w
}} 3
Cont..
Inheritance Quiz 2
public class A {
public A() { System.out.println("I'm A"); }
} // outputs
When binding is performed before the program is run, it’s called early
binding. also called [static binding] OR [compile-time binding]
When the binding occurs at run-time based on the type of object, it’s
called late binding. also called [dynamic binding] OR [run-time binding.]
That is, the compiler still doesn’t know the object type, but the method-
call mechanism finds out and calls the correct method body.
Implementation of method binding
(Polymorphism)
Method Overloading (Early Binding) static binding
Methods with same name, but the compiler uses two mechanisms to
differentiate the overloaded methods
Number of parameters
Datatype of parameters