Java Interface and Class Hierarchy

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
?*/

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭