---
title: "JAVA 实例化 父类与子类"
author: "john201314"
date: "2015-04-27T19:48:54Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2015-05-02T17:58:22Z"
id: 2206562
url: "https://john201314.iteye.com/blog/2206562"
---

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);
    }
}

大家猜一下会打印什么？

最终结果是
Dervied tell name: null
Dervied print name: null
Dervied tell name: dervied
Dervied print name: dervied