CSDN博客

img Reve

Java Interface and Class Hierarchy

发表于2004/7/6 14:37:00  1064人阅读

分类: Java

public interface IClassUpA {
?? ?public void sayA();
}

public interface IClassUpB {
??? public void sayB();
}

public interface IClassUp extends IClassUpA, IClassUpB {
??? public void say();
}

// Do NOT implements IClassUpA here!
public class ClassUpA {
??? public void sayA() {
??? ??? System.out.println("ClassUpA sayA()");
??? }
}

// Here ClassUp implements IClassUp, which extends IClassUpA and IClassUpB.
//
// Now the ClassUp's method sayA(), which extended by ClassUpA, is the
// implementation of IClassUpA#sayA().
//
// That is to say, even though ClassUpA.sayA() is not the implementation of
// IClassUpA.sayA, it becomes the implementation of IClassUpA.sayA() by its
// descendant class ClassUp through "extends ClassUpA implements IClassUp".
//
// Something interesting and somewhat confusing for some large system.
public class ClassUp extends ClassUpA implements IClassUp {
??? // No need to implement the IClassUpA.sayA() here!
??? public void say() {
??? ??? System.out.println("ClassUp say()");
??? }
??? public void sayB() {
??? ??? System.out.println("ClassUp sayB()");
??? }
}

// Test my idea.
public class InterfaceAndClassTest {
??? public static void main(String[] args) {
??? ??? Object cu = new ClassUp();
??? ??? if (cu instanceof ClassUpA) {
??? ??? ??? ClassUpA myCU = (ClassUpA) cu;
??? ??? ??? myCU.sayA();
??? ??? }
??? }
}
// Result:
// ClassUpA sayA()

// Copyright NOTICE:
// You are authorized to copy and use this example codes and comments for
// non-commercial use, under the condition that you keep? the article's copyright
// notice unchanged and ship with the notice.

// This article is firstly posted at
// http://blog.csdn.net/reve/

/*******************************************************************************
?* Copyright (c) 2002, 2004 IDSignet.
?* All rights reserved.
?* http://www.idsignet.com
?*
?* Created on Jul 6, 2004
?*******************************************************************************/

/**
?* @author Janyckee Joz
?*/
0 0

相关博文

我的热门文章

img
取 消
img