As per the rule, while overriding a method in subclass, parameters cannot be changed and have to be the same as in the super class.

What if we pass subclass of parameter while overriding method ?

Will it be called as overloading or overriding?

Based on my query I have written some code below.

I was expecting the output as "Dog eats Flesh Food" but to my surprise the output is "Animal eats Flesh Food"

Will appreciate if someone can explain how does Animal method gets called when the object assigned is of type Dog ?

class Food {

public String toString(){

return "Normal Food";

}

}

class Flesh extends Food {

public String toString(){

return "Flesh Food";

}

}

class Animal {

public void eat(Food food){

System.out.println("Animal eats "+ food);

}

}

class Dog extends Animal{

public void eat(Flesh flesh){

System.out.println("Dog eats "+ flesh);

}

}

public class MyUtil {

public static void main(String[] args) {

Animal animal = new Dog();

Flesh flesh = new Flesh();

animal.eat(flesh);

}

}

解决方案

The method eat in Dog does not override the method eat in Animal. This is because the arguments are different (one requires Flesh, the other requires Food).

The eat methods are overloads.

Choosing between overloads takes place at compile time, not runtime. It is not based on the actual class of the object on which the method is invoked, but the compile-time type (how the variable is declared).

animal has compile-time type Animal. We know this because the declaration of the variable animal was Animal animal = .... The fact that it is actually a Dog is irrelevant - it is the version of eat in Animal that must be invoked.

On the other hand, the toString method in Flesh does override the toString method in Food.

When one method overrides another it is the actual class of the object that the method is invoked on that determines which version runs.

In the eat method of Animal, even though the argument has compile-time type Food, if you pass an instance of Flesh to it, it is the toString method in Flesh that will execute.

Therefore you get the message "Animal eats Flesh Food".

JAVA传递子类参数,在Java中,是否可以通过传递超类方法中使用的参数的子类来覆盖方法?...相关推荐

  1. java多态ppt_轻松学Java之继承与多态PPT课件.pptx

    轻松学Java之继承与多态PPT课件.pptx 第8章 继承与多态,在前面我们已经了解了类的基本使用方法.对于面向对象的程序而言,它的精华在于类的继承可以以既有的类为基础,进而派生出新的类.通过这种方 ...

  2. linux shell java 参数_java调用linux中的shell脚本传递参数并返回执行结果

    [刚接触 linux下的开发,最近遇到java调用shell脚本的问题,找到一个比较适合菜鸟级的方法,转述如下: 在需要运行的SHELL脚本第一行添加 #!/bin/sh然后在终端运行  chmod ...

  3. java分布式对象——远程方法中的参数和返回值+远程对象激活

    [0]README 1)本文文字描述转自 core java volume 2, 旨在学习 java分布式对象--远程方法中的参数和返回值+远程对象激活 的相关知识: [1]远程方法中的参数和返回值 ...

  4. java两种传参,有关java参数的两种传递机制

    值传递:方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际参 数的值. 引用传递:也称为传地址.方法调用时,实际参数的引用(地址,而不是参数的值)被传递给方法中相对 ...

  5. java如何调用父类的实例对象_java子类调用父类的方法中包含子类重写的实例方法...

    java子类调用父类的方法中包含子类重写的实例方法 # 看题目是不是很绕,这个我也不知道怎么才能更简单的表达了 # 先看代码: public class Common { public static ...

  6. java开根号函数_Java中真的只有值传递么?

    原创: ITwalking 转载于公众号[ 编程大道] (本文非引战或diss,只是说出自己的理解,欢迎摆正心态观看或探讨) 回顾值传递和引用传递 关于Java是值传递还是引用传递,网上有不一样的说法 ...

  7. Java主方法引用传递_java方法中的参数传递是值传递还是引用传递(转)

    1.当参数变量为基本数据类型或者字符串时: ... ... //定义了一个改变参数值的函数 public static void changeValue(int x) { x = x *2; } .. ...

  8. 在JAVA语言程序中main_在Java程序main方法中,正确的参数是

    [单选题]下列叙述中,错误的是 [填空题]Access属于()数据库,Access中,不允许在主关键字字段中有重复值或(). [单选题]如下哪些字符串是Java中的标识符? [填空题]常用的基本电量传 ...

  9. java命令行参数工具_Java方法中的参数太多,第8部分:工具

    java命令行参数工具 在我的系列文章的前七篇文章中,有关处理Java方法中期望的参数过多的内容集中在减少方法或构造函数期望的参数数量的替代方法上. 在本系列的第八篇文章中,我将介绍一些工具,这些工具 ...

  10. java中重载 参数顺序_Java方法中的参数太多,第4部分:重载

    java中重载 参数顺序 期望将过多的参数传递给Java方法的问题之一是,该方法的客户端很难确定它们是否以适当的顺序传递了适当的值. 在以前的文章中,我描述了如何使用自定义类型 , 参数对象和构建器来 ...

最新文章

  1. 光伏企业:再出海要上两节课
  2. 华为设备ENSP静态路由的配置实战
  3. 如何让SVN提交时候强制添加注释
  4. 大家所推崇的Redis分布式锁真的就万无一失吗?
  5. 产业链布局优势明显,三星开启全新移动智能体验新时代
  6. CentOS7.9安装及配置
  7. PHP代码规范[转]
  8. 这款颜值高的到底是剃须刀还是充电宝,这么黑科技?
  9. rsyslog收集nginx日志配置
  10. 《深入浅出通信原理》学习(1-8)
  11. Boost Asio快速入门
  12. 2020-03-12
  13. 《现代汉语》北大公开课
  14. html飞机大战论文,7.HTML5--飞机大战
  15. Promethues原理详解
  16. 《数学之美》——吴军#读书笔记
  17. ffmpeg 常用命令总结:(avi转MP4、MP4转ts、视频压缩、去除视频声音、合并音频和视频)
  18. 【游戏客户端】剧情系统
  19. Description 给出一个N*N的二维数组,求出其中的最大数和其所在的行号、列号(均从0开始计算)。 Input 第1行是一个正整数N(2≤N≤10),表示二维数组的大小。后跟N行,每行N个数。
  20. Word中遇到的各种问题及解决方案

热门文章

  1. 三态门有一个信号控制端en_W25Q32JVSSIQ|哪些PCB设计会影响信号质量?
  2. qstring如何初始化_QString介绍
  3. 【SpringCloud】Eureka-实例
  4. python opencv卡尺测量边缘距离
  5. pycharm 显示 ini
  6. 最强的目标检测网络:DetectoRS 54.7 AP
  7. face.evoLVe.PyTorch
  8. MobileNetV2-YoloV3
  9. Tensorflow快速入门2--实现手写数字识别
  10. make for arm