简体   繁体   中英

new keyword in java

New to java, and still struggling with new keyword and inheritance:

public class A1 {
    void a_method (){
        System.out.println("Inside A1 method");
    }
}

public class A2 extends A1{
    void a_method(){
        System.out.println("Inside A2 method");
    }
}

public class TestA1A2 {

    public static void main(String[] args) {

        A1 a1 = new A1();
        A1 a2 = new A2();  //not sure if it created object of A1 or A2
        A2 a3 = new A2();

        a1.a_method();
        a2.a_method();
        a3.a_method();

    }
}

i am struggling to understand the new keyword and if in the above code:

A1 a2 = new A2();  //not sure if it created object of A1 or A2

will a2 be an object of A1 or A2? from the output i can see that it called the method of A2, but i really dont get the new keyword. And as A2 is extending A1, is it possile to call the method of A1??

A1 a2 = new A2(); 

This code creates a A2 object but you can only access to methods of A1 class with a2. If a method is overriden in A2, this is the method of A2 which is called.

You created a new A2() object, because you said 'new A2()'. Since A2 is also an A1, you can refer to it like it's an A1:

A1 a1 = new A2();
A2 a2 = new A2();

In the above example, in a1, you can call all the methods of A1 on a1. For a2, you can call all the methods of a1 AND a2 on it, since you inherited the methods of A1.

A1 a2 = new A2(); This will create an object of type A2.

Here what happen is..

You are creating an Object of type A2 and assign in to a reference of type A1. When you execute new A2(), it'll invoke the constructor A2() (which is the default constructor) and create an Object of type A2.

public class A2 extends A1{
     public A2(){
       super();
     }

    void a_method(){
        System.out.println("Inside A2 method");
    }
}

Since A2 extends A1 , A2 is an A1 . It is a specialized type of A1 . It is everything an A1 is, but there's probably something unique about A2 . An analogy would be how a cat IS a mammal. In Java-speak, Cat would extend Mammal .

A1 a2 = new A2();

This line creates an instance of A2 because of new A2() . However, the tricky bit here is that going forward, it's going to be treated like an A1 because of A1 a2 = . Let me remove one little bit of noise from the line:

A1 xx = new A2();

This is exactly the same, but the resulting object is called xx . Thus, we're creating a instance of the A2 class, we're calling it xx , and going forward, we'll be treating it like an A1 .

A1 is the parent class of A2,

A1 a2 = new A2();

Creates an instance of A2 whose parent class is A1. But because A2 inherited from A1, a2 is also A1.

This is called polymorphism . Since A2 extends A1 , you can declare A1 as A2 and that will give you access to all of the non overriden methods inside A1 and all the overriden A1 methods inside A2 (btw its a good practice to use the @Override annotation when you override).

In this case, because A2 overrides a_method() when you do a2.a_method() you'll be accessing A2's version of a_method() .

An explanation from the javadoc:

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

When you inherit from a class in Java the inherited class is reffered to as the super class. Think of it as the more general class. The class that inherits is more specialised.

A1 a2 = new A2();

in this case a2 (type A1) is the general class and by doing the operation you make it of a more specialised type defined in A2

A2 object is (extended) A1 object too. So you can use it as A2 and as A1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM