Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java inheritance initialization problem     - FileZilla 3.10.1.1 install on Ubuntu 14.10 (Linux)

- Ubuntu How to mount iso file (Linux)

- Linux system security knowledge (Linux)

- Manually generate AWR reports (Database)

- Some common Linux commands Study Notes (Linux)

- IOS distributed management using GitHub project development (Linux)

- CentOS install expect (Linux)

- Linux System Tutorial: Ubuntu on the desktop is disabled by default keyring to unlock tips (Linux)

- Git uses a basic tutorial (Linux)

- Using Linux strace command trace / debug a program commonly used options (Linux)

- Linux stand-alone OGG synchronous Oracle 11g DB test (Database)

- Mass data storage application of MongoDB database (Database)

- Deepin Tutorial: Depth Description Installer expert mode (Linux)

- PostgreSQL log classification and management (Database)

- How to restart after a crash Cinnamon (Linux)

- Java-based data source database access (Programming)

- Binary tree traversal algorithm summary (recursive and non-recursive) (Programming)

- SQL MySQL query table duplicate data (Database)

- Nginx concerning the location and rewrite applications proxy_pass (Server)

- CentOS 6 adds disk quota limit (Linux)

 
         
  Java inheritance initialization problem
     
  Add Date : 2017-04-13      
         
         
         
  Look at the code said the results

public class Dervied extends Base {

    private String name = "dervied";
    public Dervied () {
        tellName ();
        printName ();
    }
    public void tellName () {
        System.out.println ( "Dervied tell name:" + name);
    }
    public void printName () {
        System.out.println ( "Dervied print name:" + name);
    }
    public static void main (String [] args) {
        new Dervied ();
    }
}
class Base {
    private String name = "base";
    public Base () {
        tellName ();
        printName ();
    }
    public void tellName () {
        System.out.println ( "Base tell name:" + name);
    }
    public void printName () {
        System.out.println ( "Base print name:" + name);
    }
}
The result:
Dervied tell name: null
Dervied print name: null
Dervied tell name: dervied
Dervied print name: dervied

Why is this so
Regardless of the name and modify Base is public or protected, the results are like this.
Even the code into this, or the result (name field Base class changed basename)

public class Dervied extends Base {

    private String name = "dervied";
    public Dervied () {
        tellName ();
        printName ();
    }
    public void tellName () {
        System.out.println ( "Dervied tell name:" + name);
    }
    public void printName () {
        System.out.println ( "Dervied print name:" + name);
    }
    public static void main (String [] args) {
        new Dervied ();
    }
}
class Base {
    public String basename = "base";
    public Base () {
        tellName ();
        printName ();
    }
    public void tellName () {
        System.out.println ( "Base tell name:" + basename);
    }
    public void printName () {
        System.out.println ( "Base print name:" + basename);
    }
}

God gave away a large background
Constructor Base, two methods tellName and printName calls, because inheritance reasons, it should do is subclass method, the subclass this case has been initiated, the data members, so the display is null.
     
         
         
         
  More:      
 
- To teach you a trick to find the real IP address (Linux)
- Comparison of sorting algorithms (Programming)
- Five programming fallacy (Programming)
- Use Python to perform system commands common usage (Programming)
- Linux with Windows Explorer as a security system (Linux)
- Open MySQL slow query log (Database)
- Ubuntu System Log Configuration / var / log / messages (Linux)
- Graphic Git (Linux)
- Ubuntu comes with gedit editor to add Markdown preview widget (Linux)
- How to use the command line to obtain Freely RSS source on Linux (Linux)
- Compile and install Ubuntu Linux 4.0.5 kernel, network and fix vmware kernel module compilation error (Linux)
- GAMIT10.5 under CentOS installation (Linux)
- Use apt-p2p up a local Debian package cache (Server)
- Simple security measures to reinforce the Linux kernel (Linux)
- Linux-du and df command (Linux)
- QBit development of micro-services (Server)
- Redis 3.0.3 Cluster Setup (Database)
- Linux Network Programming - raw socket instance: MAC Address Scanner (Programming)
- MongoDB study notes - polymerization (Database)
- Python calls the API interface in several ways (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.