Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2076337
  • 博文数量: 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:38:29

The Essence of OOP Using Java, Polymorphism and Interfaces, Part 1
 
Method overloading

I covered method overloading as one form of polymorphism (compile-time polymorphism) in a previous lesson.  I also explained automatic type conversion and the use of the cast operator for type conversion 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 lesson and the next, I will explain 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 will try to take it slow and easy.  As usual, I will illustrate the concept using sample programs.

I will also tie this concept back to the concept of polymorphism using method overriding through inheritance.

A skeleton program

I will present a simple skeleton program that illustrates many of the important aspects of polymorphic behavior based on the Java interface.

Multiple inheritance and the cardinal rule

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

I will explain the cardinal rule of interface implementation.

A new relationship

I will explain 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 will explain 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 will explain 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 will suggest that there is little if anything useful that can be done in Java without understanding and using interfaces.

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

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