W
Will
Thomas, I compiled and ran your code.
Indeed, an array of objects that all
implement the same interface does exclude
non implementer classes. I cast one to the
Interface it compiled, but it did not run.
Here is the code (based on yours):
public interface Airborne
{
void fly(int feet);
}
public class Bird implements Airborne
{
public void fly(int feet) { }
public void chirp() { }
}
public class Bug implements Airborne
{
public void fly(int feet) { }
public void run(int feet) { }
}
public class Mammals
{
public void run(int feet) { }
}
public class Tester
{
public static void main(String [] args) {
Bird bird1 = new Bird();
Bug bug1 = new Bug();
Mammals mammals1 = new Mammals();
Airborne [] flyers = {bird1, bug1, (Airborne)mammals1};
flyers[0].fly(2);
flyers[1].fly(7);
flyers[2].fly(9);
}
}
Compiles OK but won't run (as expected
because mammals is not an Airborne)
OUTPUT:
java.lang.ClassCastException: Mammals
at Tester.main(Tester.java:9)
Press any key to continue...
I see that that is useful in OO.
Below is a practical example of an interface
being used to compare objects. (Comparator)
At NO POINT is the Interface method called
from the code. But it is being called in
reality (its called from somewhere 5 times
- I have tested it)
This required Interface method is called:
-public int compareTo(some Params)-. What
or who is calling it 5 times?
Note, in my sphere of thinking it (the
interface method) is actually doing some
work. But I understood that Interface methods
have no implementation. Yet it compared
objects in a complicated way 5 times!
Here is the working code: (I tested it)
class Person implements Comparable {
private String firstName;
private String lastName;
private int age;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Object anotherPerson) throws ClassCastException {
if (!(anotherPerson instanceof Person))
throw new ClassCastException("A Person object expected.");
int anotherPersonAge = ((Person) anotherPerson).getAge();
return this.age - anotherPersonAge;
}
}
import java.util.Arrays;
public class Testing {
public static void main(String[] args) {
Person[] persons = new Person[4];
persons[0] = new Person();
persons[0].setLastName("Goodyear");
persons[0].setAge(56);
persons[1] = new Person();
persons[1].setLastName("Clark");
persons[1].setAge(8);
persons[2] = new Person();
persons[2].setLastName("Graff");
persons[2].setAge(16);
persons[3] = new Person();
persons[3].setLastName("Goodyear");
persons[3].setAge(69);
System.out.println("Natural Order");
for (int i=0; i<4; i++) {
Person person = persons;
String lastName = person.getLastName();
int age = person.getAge();
System.out.println(lastName +". Age:" + age);
}
Arrays.sort(persons);
System.out.println();
System.out.println("Sorted by age");
for (int i=0; i<4; i++) {
Person person = persons;
String lastName = person.getLastName();
int age = person.getAge();
System.out.println(lastName +". Age:" + age);
}
}
}
Indeed, an array of objects that all
implement the same interface does exclude
non implementer classes. I cast one to the
Interface it compiled, but it did not run.
Here is the code (based on yours):
public interface Airborne
{
void fly(int feet);
}
public class Bird implements Airborne
{
public void fly(int feet) { }
public void chirp() { }
}
public class Bug implements Airborne
{
public void fly(int feet) { }
public void run(int feet) { }
}
public class Mammals
{
public void run(int feet) { }
}
public class Tester
{
public static void main(String [] args) {
Bird bird1 = new Bird();
Bug bug1 = new Bug();
Mammals mammals1 = new Mammals();
Airborne [] flyers = {bird1, bug1, (Airborne)mammals1};
flyers[0].fly(2);
flyers[1].fly(7);
flyers[2].fly(9);
}
}
Compiles OK but won't run (as expected
because mammals is not an Airborne)
OUTPUT:
java.lang.ClassCastException: Mammals
at Tester.main(Tester.java:9)
Press any key to continue...
I see that that is useful in OO.
Below is a practical example of an interface
being used to compare objects. (Comparator)
At NO POINT is the Interface method called
from the code. But it is being called in
reality (its called from somewhere 5 times
- I have tested it)
This required Interface method is called:
-public int compareTo(some Params)-. What
or who is calling it 5 times?
Note, in my sphere of thinking it (the
interface method) is actually doing some
work. But I understood that Interface methods
have no implementation. Yet it compared
objects in a complicated way 5 times!
Here is the working code: (I tested it)
class Person implements Comparable {
private String firstName;
private String lastName;
private int age;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Object anotherPerson) throws ClassCastException {
if (!(anotherPerson instanceof Person))
throw new ClassCastException("A Person object expected.");
int anotherPersonAge = ((Person) anotherPerson).getAge();
return this.age - anotherPersonAge;
}
}
import java.util.Arrays;
public class Testing {
public static void main(String[] args) {
Person[] persons = new Person[4];
persons[0] = new Person();
persons[0].setLastName("Goodyear");
persons[0].setAge(56);
persons[1] = new Person();
persons[1].setLastName("Clark");
persons[1].setAge(8);
persons[2] = new Person();
persons[2].setLastName("Graff");
persons[2].setAge(16);
persons[3] = new Person();
persons[3].setLastName("Goodyear");
persons[3].setAge(69);
System.out.println("Natural Order");
for (int i=0; i<4; i++) {
Person person = persons;
String lastName = person.getLastName();
int age = person.getAge();
System.out.println(lastName +". Age:" + age);
}
Arrays.sort(persons);
System.out.println();
System.out.println("Sorted by age");
for (int i=0; i<4; i++) {
Person person = persons;
String lastName = person.getLastName();
int age = person.getAge();
System.out.println(lastName +". Age:" + age);
}
}
}