Analysis of java upward transformation

Hits: 0

Upward transformation

Classic Case

public class Animal {
    public void eat(){
        System.out.println("animal eatting...");
    }
}

public class Cat extends Animal{

    public void eat(){
System.out 
        .println ( "I eat fish" );
    }
}

public class Dog extends Animal{

    public void eat(){
System.out 
        .println ( "I eat bones" );
    }

    public void run(){
        System.out.println ( "I will run" ) ;
    }
}

public class Main {

    public static void main(String[] args) {

        Animal animal = new Cat(); //Upcast
        animal.eat();

        animal = new Dog();
        animal.eat();
    }

}

//result: 
//I eat fish 
//I eat bones

When the parent class object reference variable refers to the subclass object, the type of the referenced object determines whose member method is called, and the reference variable type determines the method that can be called. Animal is the reference variable type, which determines which methods can be called; eat()methods can be called, and cat is the type of the referenced object, which determines whose method is called: the method that calls cat.

Upward transformation

  • Converting an object referenced by a subclass to the superclass type is called upcasting. In layman’s terms, it is to convert a subclass object into a parent class object. Here the parent class object can be an interface.
  • Animal animal = new Cat();Convert the subclass object Cat to the parent class object Animal. At this time, the method called by the animal reference is a subclass method.

Issues to be aware of when transitioning upwards

  • When upcasting, methods defined separately by subclasses are lost. For example, in the run method defined in the Dog class above, when the animal reference points to an instance of the Dog class, the run method cannot be accessed, and animal.run()an error will be reported.

  • Subclass references cannot point to superclass objects. Cat c = (Cat)new Animal()This is not acceptable.

  1. Assign the subclass object to the superclass object, and the superclass object becomes the upcast object of the subclass, but this can only access methods and variables inherited from the superclass or overridden methods.
  2. Only the upcast object can call (access) the members related to the parent class in the subclass, and the members defined later in the subclass cannot be called (variables or methods)

The Benefits of Upward Transformation

  • Reduce repetitive code and make code concise.
  • Improve system scalability.
    For example: For example, I now have many kinds of animals, and I want to feed them. Without upcasting, I would need to write:

public void eat(Cat c){
    c.eat();
}

public void eat(Dog d){
    d.eat();
}
//......

eat(new Cat());
eat(new Cat());
eat(new Dog());
//......

Write one method for one animal. If I have 10,000 kinds of animals, I have to write 10,000 methods. After writing, the year of the monkey, the horse, and the month have passed several times. Well, you are amazing, you finished writing it patiently, thought you could relax for a while, suddenly a new animal came, do you have to write a separate eat method for it again? Are you happy?
What if I use upcasting? I just need to write this:

public void eat(Animal a){
    a.eat();
}

eat(new Cat());
eat(new Cat());
eat(new Dog());
//.....

Well, done. Is the code much simpler? And at this time, if I add a new animal, I only need to implement its own class and let him inherit Animal, instead of writing a separate eat method for it. Does it improve scalability?

class Fuc{
    public int num=100;
    public void show(){
        System.out.println("show Fuc");
    }
}

class  Zic  extends  Fuc {
     public  int num= 1000 ;
    public  int num2= 200 ;

    @Override
    public void show() {
        System.out.println("show Zic");
    }
}

public class DuoTai {
    public static void main(String[] args) {
        Fuc f=new Zic();System.out.println(f.num);

        //System.out.println(f.num2 ) ; compile error
        f.show();
    }
}

The compiler will first look at the parent class during the compilation phase, and the compiler will find the matching method in the method table of the type in the method area by declaring the type of the object (that is, the type of the reference itself). So at first, there is no num2 variable in the parent class. You can use the reference of the parent class to access the variable of the child class. The compilation will not pass. The child class can access the parent class, but the parent class cannot access the child class because the child class inherits the parent class. In layman’s terms, the son can visit the father, but your father cannot visit the son.

static method call

class  A {

public static void show(){
        System.out.println("hhhh");
    }

}

class B extends A {

    public static void show(){
        System.out.println ( "You Ha" ) ;
    }

}
public class C{

    public static void main(String[] args) {
        A b=new B();
        b.show();
    }
}

Static methods do not have polymorphism, because
static is related to classes and has nothing to do with object instances. Static methods can be inherited and overridden, but overriding is only formal (not rewriting), and the parent class method is not overridden. . The same name will be hidden. The static method is hidden rather than overridden, so when the upcast variable calls this static method, it calls the static method inherited and hidden in the subclass. A deep understanding is that the static method belongs to static binding. During the compilation process In , the method is bound to the reference variable type, and the compiler sees the parent class, so the declared type prevails at runtime, and the upcast variable calls the inherited and hidden static method.

You may also like...

Leave a Reply

Your email address will not be published.