Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2089345
  • 博文数量: 519
  • 博客积分: 10070
  • 博客等级: 上将
  • 技术积分: 3985
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-29 14:05
个人简介

只问耕耘

文章分类

全部博文(519)

文章存档

2016年(1)

2013年(5)

2011年(46)

2010年(220)

2009年(51)

2008年(39)

2007年(141)

2006年(16)

我的朋友

分类: Java

2010-01-14 11:41:18

The Essence of OOP using Java, Polymorphism and Interfaces, Part 2
Method overloading

I covered method overloading as one form of polymorphism (compile-time polymorphism) in a previous lesson.

Method overriding and class inheritance

I discussed runtime polymorphism implemented through method overriding and class inheritance in more than one previous lesson.

Using the Java interface

In this and the previous lesson, I am explaining runtime polymorphism as implemented using method overriding and the Java interface.

A very important concept

In my opinion, this is one of the most important concepts in Java OOP, and the one that seems to give students the greatest amount of difficulty.  Therefore, I am trying to take it slow and easy.  As usual, I am illustrating the concept using sample programs.

A skeleton program

In the previous lesson, I presented a simple skeleton program that illustrated many of the important aspects of polymorphic behavior based on the Java interface.

Multiple inheritance and the cardinal rule

I explained how the implementation of interfaces in Java is similar to multiple inheritance.

I explained the cardinal rule of interface implementation.

A new relationship

I explained that objects instantiated from classes that implement the same interface have a new relationship that goes beyond the relationship imposed by the standard class hierarchy.

One object, many types

I explained that due to the combination of the class hierarchy and the fact that a class can implement many different interfaces, a single object in Java can be treated as many different types.  However, for any given type, there are restrictions on the methods that can be invoked on the object.

Many classes, one type

I explained that because different classes can implement the same interface, objects instantiated from different classes can be treated as a common interface type.

Interfaces are critical to Java programming

I suggested that there is little if anything useful that can be done in Java without understanding and using interfaces.

In support of this suggestion, I discussed several real-world examples of the use of the Java interface, including the Delegation Event Model and Remote Method Invocation.

Another sample program

In this lesson, I will present another sample program that will take you deeper into the world of polymorphism as implemented using the Java interface.

The sample program that I will discuss in this lesson will illustrate (in a very basic form) some of the things that you can do with interfaces, along with some of the things that you cannot do with interfaces.  In order to write programs that do something worthwhile, you will need to extend the concepts illustrated by this sample program into real-world requirements.

阅读(340) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~