Programming Tutorials

(追記) (追記ここまで)

Method Override sample in Java

By: Ganesh Iyer in Java Tutorials on 2009年06月05日 [フレーム]

A method is said to be overridden when one is in parent class and another is in child class with the same name, same return type, same parameter.

class
Animal
{
 int height=10;
 int weight=20;
void talk()
 {
 System.out.println(\"Animal talking\");
 }
 void food()
 {
 System.out.println(\"Animal Eating\");
 }
}
class Cat extends Animal
{
 void talk()
 {
 System.out.println(\"meo...
meo\"); 
 }
 void food()
 {
 System.out.println(\"Drink MILK\");
 }
}
class functionOverride
{
 public static void main(String[] args)
 {
 Animal a = new Animal();
 Cat c = new Cat();
 /* c.height=20 */;
 c.talk();
 c.food();
 System.out.println(\"Height \"+c.height);
 }
}



(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /