i have a bit a weak understanding of those 2 concepts. could you explain to me what is happening in that part of the code?
public class Part5{
public static void main(String[] args) {
// Object[] zoo=new Object[4];
// zoo[0]=new Cat();
Object[] animals=new Object[4];
animals[0]=new Cat();
animals[1]=new Dog();
animals[2]=new Duck();
animals[3]=new Cow();
for (int i = 0; i < animals.length; i++) {
if(i==0)
((Cat)animals[i]).makeNoise();
if(i==1)
((Dog)animals[i]).makeNoise();
if(i==2)
((Duck)animals[i]).makeNoise();
if(i==3)
((Cow)animals[i]).makeNoise();
}
System.out.println("============");
for (int i = 0; i < animals.length; i++) {
if(animals[i] instanceof Cat)
((Cat)animals[i]).makeNoise(); // whats happening here?
if(animals[i] instanceof Dog)
((Dog)animals[i]).makeNoise();
if(animals[i] instanceof Duck)
((Duck)animals[i]).makeNoise();
if(animals[i] instanceof Cow)
((Cow)animals[i]).makeNoise();
}
}
}
class Cat {
public void makeNoise(){
System.out.println("Miyau...");
}
}
class Dog
{
public void makeNoise(){
System.out.println("HowHow....");
}
}
class Duck {
public void makeNoise(){
System.out.println("GAAGAa.....");
}
}
class Cow {
public void makeNoise(){
System.out.println("Moooooooo......");
}
}
another question
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
A a=new D();
a.foo(50);
B b=(B)a;
b.foo(10);
}
}
abstract class A{
final int NUMBER_OF_TIME=100;
abstract void foo(int x);
final void doIt(){
System.out.println("bla");
}
}
class B extends A{
@Override
void foo(int i) {
System.out.println("B"+i);
}
}
class D extends B{
void foo(int t) {
super.foo(t); // why do i need to use super. here? what would happen if i didnt?
System.out.println("D");
}
}