0% found this document useful (0 votes)
42 views

Java Inheritance Example

The document discusses Java inheritance through several examples: 1) A Programmer class inherits from an Employee class, allowing a Programmer object to access the salary field of the Employee class. 2) A MountainBike class inherits from a Bicycle class, gaining its fields and methods while also adding a seatHeight field and modification method. 3) In Java, the constructor of the base class is automatically called in the derived class constructor if it has no arguments. To call a parameterized base class constructor, it must be the first line of the derived class constructor.

Uploaded by

banan nasih
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Java Inheritance Example

The document discusses Java inheritance through several examples: 1) A Programmer class inherits from an Employee class, allowing a Programmer object to access the salary field of the Employee class. 2) A MountainBike class inherits from a Bicycle class, gaining its fields and methods while also adding a seatHeight field and modification method. 3) In Java, the constructor of the base class is automatically called in the derived class constructor if it has no arguments. To call a parameterized base class constructor, it must be the first line of the derived class constructor.

Uploaded by

banan nasih
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Java Inheritance Example

As displayed in the above figure, Programmer is the subclass and Employee is the
superclass.
The relationship between the two classes is Programmer IS-A Employee. It means
that Programmer is a type of Employee.

class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

Output :
Programmer salary is: 40000.0
Bonus of programmer is: 10000

In the above example, Programmer object can access the field of own class as well
as of Employee class i.e. code reusability.

There is another example below :

Here is the sample code for a possible implementation of a Bicycle class that was
presented in the Classes and Objects lesson:
public class Bicycle {

// the Bicycle class has three fields


public int cadence;
public int gear;
public int speed;

// the Bicycle class has one constructor


public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

// the Bicycle class has four methods


public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}

public void applyBrake(int decrement) {


speed -= decrement;
}

public void speedUp(int increment) {


speed += increment;
}

A class declaration for a MountainBike class that is a subclass of Bicycle might look


like this:
public class MountainBike extends Bicycle {

// the MountainBike subclass adds one field


public int seatHeight;

// the MountainBike subclass has one constructor

public MountainBike(int startHeight,


int startCadence,
int startSpeed,
int startGear) {
super(startCadence, startSpeed, startGear);

seatHeight = startHeight;
}

// the MountainBike subclass adds one method


public void setHeight(int newValue) {
seatHeight = newValue;
}
}

MountainBike inherits all the fields and methods of Bicycle and adds the


field seatHeight and a method to set it. Except for the constructor, it is as if you
had written a new MountainBike class entirely from scratch, with four fields and
five methods. However, you didn't have to do all the work. This would be
especially valuable if the methods in the Bicycle class were complex and had
taken substantial time to debug.

Inheritance and constructors in Java


In Java, constructor of base class with no argument gets automatically called in derived
class constructor. For example, output of following program is:

Base Class Constructor Called


Derived Class Constructor Called

// filename: Main.java

class Base {

  Base() {

    System.out.println("Base Class Constructor Called ");

  }

  

class Derived extends Base {

  Derived() {

    System.out.println("Derived Class Constructor Called


");

  }
}

  

public class Main {

  public static void main(String[] args) {  

    Derived d = new Derived();

  }

But, if we want to call parameterized contructor of base class, then we can call it
using super(). The point to note is base class constructor call must be the first
line in derived class constructor. For example, in the following program,
super(_x) is first line derived class constructor.

// filename: Main.java

class Base {

  int x;

  Base(int _x) {

    x = _x;

  }

  
class Derived extends Base {

  int y;

  Derived(int _x, int _y) {

    super(_x);

    y = _y;

  }

  void Display() {

    System.out.println("x = "+x+", y = "+y);

  }

  

public class Main {

  public static void main(String[] args) {  

    Derived d = new Derived(10, 20);

    d.Display();

  }

Output:
x = 10, y = 20
Terms used in Inheritance

o Class: A class is a group of objects which have common properties. It is a


template or blueprint from which objects are created.

o Sub Class/Child Class: Subclass is a class which inherits the other


class. It is also called a derived class, extended class, or child class.

o Super Class/Parent Class: Superclass is the class from where a


subclass inherits the features. It is also called a base class or a parent class.

o Reusability: As the name specifies, reusability is a mechanism which


facilitates you to reuse the fields and methods of the existing class when
you create a new class. You can use the same fields and methods already
defined in the previous class.
The syntax of Java Inheritance

class Subclass-name extends Superclass-name  

{  
   

//methods and fields  

}  

The extends keyword indicates that you are making a new class that derives from
an existing class. The meaning of "extends" is to increase the functionality.

In the terminology of Java, a class which is inherited is called a parent or


superclass, and the new class is called child or subclass.

You might also like