Java Interface and Class Hierarchy

来源:互联网 发布:nhj软件 编辑:程序博客网 时间:2024/05/22 08:56
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
?*/
原创粉丝点击