overriding

Java cast to superclass and call overload method

南笙酒味 提交于 2019-12-02 08:04:41
abstract class A { int met(A a) { return 0; } int met(B b) { return 1; } int met(C c) { return 2; } } class B extends A { int met(A a) { return 3; } int met(B b) { return 4; } int met(C c) { return 5; } } class C extends B { int f() { return ((A)this).met((A)this); } } public class teste { public static void main(String args[]) { C x = new C(); System.out.println(x.f()); } } The program will return 3 and I was expecting 0. Why does the first cast in the method f do nothing and the second one works? Is it because in the A and B classes the met methods are overloaded and therefore static binding

How does value of (String… args) affect in subclass overriding?

China☆狼群 提交于 2019-12-02 08:00:49
问题 if a class Alpha has a method with arguments as public void foo(String... args) & same method is tried to be overridden in a sub-class Beta by say public void foo(String a) .And a reference object is created & instantiated to subclass Beta & trying to access foo method from Alpha. Its returning the value from Alpha class instead of the value from Beta Sub-class . Shouldnt it return the value from BETA class as its initialized & assigned Beta class Object ? What is the difference b/w Method

how to override a method in java and then call the superclass method

筅森魡賤 提交于 2019-12-02 07:30:35
I am trying to override a method in the superclass in my java subclass and then call the overriden method from the superclass. Is this the correct way to do it. The method in the super class i want to overrride is called describe() public static void describe() { Item.describe(); } Use the super keyword. @Override public void describe() { super.describe(); } Note the annotation which although is not necessary is "correct". As was pointed out, static methods do not get inherited. It's possible to imitate polymorphic behavior with static methods using reflection but I do not recommend this for a

SCJP - override method with exception handling raises a compiler error

我只是一个虾纸丫 提交于 2019-12-02 06:19:09
In the SCJP book by Kathey Sierra, an excerpt is as follows: If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you’re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception (more in Chapter 5). Let’s take a look at an example: class Animal { public void eat() throws Exception { // throws an Exception } } class Dog2 extends

why we can't override a method and define it to return a superclass of the original method?

大兔子大兔子 提交于 2019-12-02 06:09:33
问题 I'm currently learning Java classes and objects from java tutorial oracle and have encountered the following statements and code. I understand the concept but I do not know why we can't override a method and define it to return a superclass of the original method? What is the reason behind it? Could someone please enlighten me? Thanks in advance for any help! You can override a method and define it to return a subclass of the original method, like this: public Number returnANumber() { ... }

python built-in functions vs magic functions and overriding [duplicate]

江枫思渺然 提交于 2019-12-02 05:59:46
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Intercept operator lookup on metaclass How can I intercept calls to python’s “magic” methods in new style classes? Consider the following code: class ClassA(object): def __getattribute__(self, item): print 'custom__getattribute__ - ' + item return '' def __str__(self): print 'custom__str__' return '' a=ClassA() print 'a.__str__: ', a.__str__ print 'str(a): ', str(a) The output was surprising to me: a.__str__:

Need to overwrite XMLWriter's method

不问归期 提交于 2019-12-02 05:42:15
问题 I need to overwrite the XMLWriter's method "WriteElementString" to not write the element if the value is empty, the code bellow didn't work, tried override and new keywords but it still goes to the framework method. public static void WriteElementString(this XmlWriter writer, string localName, string value) { if (!string.IsNullOrWhiteSpace(value)) { writer.WriteStartElement(localName); writer.WriteString(value); writer.WriteEndElement(); } } The answer was close but correct solution is:

Method overriding and inheritance

左心房为你撑大大i 提交于 2019-12-02 05:15:11
问题 public class Circle { public static final double PI = 3.141592654; protected double radius; public Circle(double radius) { this.radius = radius; } @Override public String toString() { return "Class = " + getClass().getSimpleName() + " (radius = " + radius + ")"; } } public class PlaneCircle extends Circle { private double centerX, centerY; public PlaneCircle(double radius, double centerX, double centerY) { super(radius); this.centerX = centerX; this.centerY = centerY; } @Override public

Why is the value of the instance field coming null?

我的梦境 提交于 2019-12-02 03:58:14
I have this simple piece of code. abstract class X { X() { read(); } private void read() { Object obj = new Object(); readValue(obj); } protected abstract void readValue(Object obj); } class Y extends X { Object obj = null; Y() { super(); } @Override protected void readValue(Object obj) { this.obj = obj; } void printer() { System.out.println("Object = " + obj); } } class Runner { public static void main(String[] args) { Y y = new Y(); y.printer(); } } When I run the above code, the object gets printed as null. (I get "Object = null" ) Surprisingly, in class Y when I remove null declaration

Stream closed and not reopened - Java

女生的网名这么多〃 提交于 2019-12-02 03:42:53
I have an easy 'homework' to do, but I have found a little problem with the closure of the input stream. To put it simply, I have to make a contact 'list' application in Java, just to use the polymorphism in the correct way. So I have a class Contact and a subclass Private (contact). In both class there is a modify method to change the value of the variables. public void modify() throws IOException { System.out.println("Previously name: " + name); System.out.println("Insert new name"); try(InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir) ) {