在Java中继承类中是否可以重载?

在Java中继承类中是否可以重载?,java,inheritance,overloading,Java,Inheritance,Overloading,在Java中继承类中是否可以重载?父类和子类包含相同的方法名,但参数不同。这是超载吗 类父类{ 公共无效添加(int a){ System.out.println(“我是父母”+a); } } 类子级扩展父级{ 公共无效添加(长a){ System.out.println(“我是孩子”); } } 是。在扩展任何类时,在内部它意味着父类的所有可访问行为都将在子类中出现或继承。i、 因此,在您的例子中,相同的名称和不同的参数是重载的。是的,当然,在Java中继承类中重载是可能的。Java编译器检测

在Java中继承类中是否可以重载?父类和子类包含相同的方法名,但参数不同。这是超载吗

类父类{
公共无效添加(int a){
System.out.println(“我是父母”+a);
}
}
类子级扩展父级{
公共无效添加(长a){
System.out.println(“我是孩子”);
}
}

是。在扩展任何类时,在内部它意味着父类的所有可访问行为都将在子类中出现或继承。i、 因此,在您的例子中,相同的名称和不同的参数是重载的。

是的,当然,在Java中继承类中重载是可能的。Java编译器检测到add方法有多个实现。因此,java编译器将根据参数确定必须执行哪个方法

class Parent {
    public void add(int a) {
        System.out.println("I am parent " + a);
    }
}

class Child extends Parent {
    public void add(long a) {
        System.out.println("I am child.");
    }
}
class Demo{
    public static void main(String args[]){

    Child child = new Child();
    child.add(1); // prints "I am parent 1"
    child.add(1L); // prints "I am child."

    }
}

您是否尝试调用
Child c=new Child();c、 增加(1);c、 添加(1L)?你已经拥有了自己去发现答案的一切。然后,对
Parent p=new Child()执行相同的操作;p、 增加(1);p、 添加(1L)是,这是重载。