Posted by : Rhyf Ahmad Thursday, May 21, 2015

A very important part of object-oriented programming enables you to create a new class based on a class that has already been defined. The class that you use as the base for your new class can be one that you have defined, a standard class in Java, or a class defined by someone else — perhaps from a package supporting a specialized application area.
This chapter focuses on how you can reuse existing classes by creating new classes based on the ones you have and explores the ramifications of using this facility, and the additional capabilities it provides. You also delve into an important related topic — interfaces — and how you can use them.
In summary, when you derive a new class from a base class, the process is additive in terms of what makes up a class definition. The additional members that you define in the new class establish what makes a derived class object different from a base class object. Any members that you define in the new class are in addition to those that are already members of the base class. For your Spaniel class that you derived from Dog, the data members to hold the name and the breed that are defined for the class Dog are  automatically in the class Spaniel. A Spaniel object always has a complete Dog object inside it — with all its data members and methods. This does not mean that all the members defined in the Dog class are available to methods that are specific to the Spaniel class. Some are and some aren't. The inclusion of members of a base class in a derived class so that they are accessible in that derived class is called class inheritance. An inherited member of a base class is one that is accessible within the derived class. If a base class member is not accessible in a derived class, then it is not an inherited member of the derived class, but base class members that are not inherited still form part of a derived class object.
An inherited member of a derived class is a full member of that class and is freely accessible to any method in the class. Objects of the derived class type contain all the inherited members of the base class — both fields and methods, as well as the members that are specific to the derived class. Remember that a derived class object always contains a complete base class object within it, including all the fields and methods that are not inherited. The next step is to take a closer look at how inheritance works and how the access attribute of a base class member affects its visibility in a derived class. You need to consider several aspects of defining and using a derived class. First of all, you need to know which members of the base class are inherited in the derived class. I explain what this implies for data members and methods separately — there are some subtleties here you need to be quite clear on. I also look at what happens when you create an object of the derived class. There are some wrinkles in this context that require closer consideration. Let's start by looking at the data members that are inherited from a base class.

Contents:

Using Existing Classes
Class Inheritance
  1. Inheriting Data Members
  2. Inherited Methods
  3. Overriding a Base Class Method

The @Override Annotation
Choosing Base Class Access Attributes
Polymorphism
  1. Using Polymorphism

Multiple Levels of Inheritance
Abstract Classes
The Universal Superclass
  1. The toString() Method
  2. Determining the Type of an Object
  3. Duplicating Objects

Methods Accepting a Variable Number of Arguments
  1. Limiting the Types in a Variable Argument List

Casting Objects
  1. When to Cast Objects
  2. Identifying Objects

More on Enumerations
  1. Adding Members to an Enumeration Class

Designing Classes
  1. A Classy Example

Using the final Modifier
Interfaces
  1. Encapsulating Constants in a Program
  2. Interfaces Declaring Methods
  3. Extending Interfaces
  4. Using Interfaces
  5. Interface Types as Method Parameters
  6. Nesting Classes in an Interface Defi nition
  7. Interfaces and the Real World

Anonymous Classes 



Ebook Preview
Get the ebook now (1.28 MB)

If the link above doesn't work, try the link below


Mirror: Get the ebook now (1.28 MB)

Leave a Reply

You can leave a comment to request any article(s), software(s), ebook(s) or report the dead link.
Then I will reply as soon as I can :)

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Recent Post

Recent Posts Widget
Instagram

- Copyright © Madriva -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -