---
title: "面试题 java类加载机制"
author: "J2EE大鸟"
date: "2013-03-07T16:33:35Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2013-03-07T16:33:35Z"
id: 1825729
url: "https://benworld.iteye.com/blog/1825729"
---

```
public class SuperA {
	public void printX(){
		System.out.println("hello superA!");
	}
}
```

 

```
public class SuperB extends SuperA {
	public void printY(){
		System.out.println("hello superB!");
	}
}
```

 

```
public class SuperTest {

	public static void main(String[] args) {
		SuperA a = new SuperB();
		SuperB b = new SuperB();
		a.printX();
		b.printY();
		b.printX();
	}

}
```

 

在a中不能调用printY的方法，因为声明时，声明的是SuperA 的类信息，a 作为对象引用只能从指向的SuperB的对象中，取得printfX方法，而不能获得superb的方法。（由于是继承关系，所以在superB中存在superA的方法）

 

而SuperB b = new SuperB(); 声明时，superB 的类信息包含了superA 的类信息，所以可以调用superA的方法。