Java: Learn Java in One Day and Learn It Well. Java for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 4) by Chan Jamie & LCF Publishing

Java: Learn Java in One Day and Learn It Well. Java for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 4) by Chan Jamie & LCF Publishing

Author:Chan, Jamie & LCF Publishing [Chan, Jamie]
Language: eng
Format: epub
Publisher: Learn Coding Fast
Published: 2016-09-29T16:00:00+00:00


8.1.2 Writing the Child Class

Now that we have completed the Member class, let us learn how to derive a class from it. Derived classes are known as child classes or subclasses, while the classes from which they are derived are known as parent classes, base classes or superclasses.

To recap, our parent class (Member ) has the following contents:

Fields

public String welcome = "Welcome to ABC Fitness";

protected double annualFee;

private String name;

private int memberID;

private int memberSince;

private int discount;

Constructor s

public Member()

public Member(String pName, int pMemberID, int pMemberSince)

Methods

public double getDiscount()

public void setDiscount()

public void displayMemInfo()

public void calculateAnnualFee()

We shall derive two classes – NormalMember and VIPMember – from the Member class.

First, let’s declare the child class NormalMember .

Add a new Java class to the inheritancedemo package and name it NormalMember . Notice that when you generate a new class, NetBeans automatically declares the class as

public class NormalMember {

}

for you?

We need to indicate that NormalMember is derived from the Member class by adding the words extends Member to the class declaration as shown below:

public class NormalMember extends Member {

}

extends is a Java keyword used to indicate that one class is inherited from another class. In our example, the NormalMember class is inherited from the Member class.

We have to use the extends keyword whenever we want to show that one class is inherited from another class. The only exception is when inheriting from the Object class. As all classes in Java are inherited from the Object class, there is no need for us to state this inheritance explicitly.

When one class is inherited from another class, it inherits all the public and protected fields and methods from the parent class. This means that the child class can use these fields and methods as if they are part of its own code; we do not have to declare these fields and methods again in the child class. In other words, even though we have not started coding our child class yet, it already has two fields (welcome and annualFee ) and four methods (getDiscount , setDiscount , displayMemInfo and calculateAnnualFee ) inherited from the parent class. This facilitates code reuse and is especially valuable if the parent class has a large number of public/protected fields and methods that the child class can use.

However, the child class does not inherit the private fields and methods of the parent class. This means that the child class will not be able to access these private fields and methods directly, it’ll have to access them using other methods. We’ll see an example of this later.

Now that we have declared NormalMember as a child class of Member , we need to write the constructor for the child class.

Whenever we write the child class constructor, it is a must for us to call the parent class’ constructor first. If we do not do that, Java will automatically call the parameterless constructor in the parent class for us.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.