public class animal
{
public String talk()
{return "animal noise";
}
}
_____________________________
public class dog extends animal
{
public String talk()
{return "bark";
}
public String talk2()
{return "bark-bark";
}
}
_____________________________
public class main
{
public static void main(String args[]) throws IOException
{dog b = new dog();
System.out.println(b.talk()); // ok
System.out.println(b.talk2()); // ok
}
}
______________________________________
public class main
{
public static void main(String args[]) throws IOException
{animal b = new dog();
System.out.println(b.talk()); // ok and will bark
System.out.println(b.talk2()); // won't work - animal doesn't have talk2
}
}
________________________________
public class main5
{
public static void main(String args[]) throws IOException
{animal b = new dog();
System.out.println(b.talk());
System.out.println((dog)(b).talk2()); // won't work - does b.talk2 first
}
}
Thought above would work - cast b to a dog to let it know it is a
dog and can access dog methods in addition to animal methods
______________________________________
Concept above was correct - syntax was wrong
Must be done like this
public class main5
{
public static void main(String args[]) throws IOException
{dog b = new dog();
System.out.println(b.talk());
System.out.println(((dog)b).talk2());
}
}