Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

SIT Internal

Inheritance
SIT Internal

Learning outcomes

• What is Inheritance
• Definition and code application
• Impact on Constructors
• Impact of Visibility modifiers
SIT Internal

What is inheritance?

a class reuses the properties and behaviors of another


class or specifies new implementation

What does it mean?


SIT Internal

Let’s look at an example

Vehicle
SIT Internal

Let’s look at another example


SIT Internal

What is inheritance?

• Grouping together common actions and attributes of an object


into a separate parent group

• Extracting common methods and attributes from classes into a


parent class
SIT Internal

What is inheritance?
• Derive a new class from an existing one

• Allows classes to use properties and behaviors from other


classes
SIT Internal

What is inheritance? ClothingItem


- brand
- size
- color
• Derive a new class from an
existing one
Shirt
• Allows classes to use properties -
-
sleeveLength
collorType
Pants
- waistSize
and behaviors from other classes. - inSeamLength
SIT Internal

Retail Store using OOPs

Shirt Pants
- brand -brand
- size - size
- color - color
- sleeveLength - waistSize
- collorType - inSeamLength
SIT Internal

Retail Store using OOPs


ClothingItem

Shirt Pants
- brand - brand
- size - size
- color - color
- sleeveLength - waistSize
- collorType - inSeamLength
SIT Internal

Hierarchy of classes
ClothingItem
- brand Base class/Parent Class
- size
- color

Shirt
Pants
- sleeveLength
- collorType - waistSize
- inSeamLength

Derived class/Sub Class


SIT Internal

Vehicle
Root class
- fuel

Hierarchy of Classes
- noOfTires
+ drive()

• Inverted tree Truck Car Motorcycle

• Any number of levels.


Limousine Sedan
SIT Internal

Inheritance: Constructors
• Constructors are not inherited, even though they have public visibility

• parent's constructor are used to set up the "parent's part" of the object

• The first line of a child’s constructor should use the reference to call the
parent’s constructor
SIT Internal

Inheritance: Constructors
The super reference can be used

• To invoke the parent's constructor

• Reference other variables and methods defined in the parent’s


class
SIT Internal

Inheritance: Access Modifiers


Private Protected Public
SIT Internal

class Vehicle{ class Car extends Vehicle{


public String color;
private int wheels; Car(){
super();
Vehicle(){
color = "red";
wheels = 4; }
Car(String color){
} this.color = color;
}
Vehicle(int wheels){ Car (String color, int wheels){
super(wheels);
this.wheels = wheels; this.color = color;
}
}
public void displayParentInfo(){
public int getWheels(){ super.print();
int a = super.wheels;
return wheels; }
}
}

The field Vehicle.wheels is not visible


public void setWheels(int wheels){

this.wheels = wheels;

public void print(){

System.out.println("Wheels: "+wheels);

}
SIT Internal

class Vehicle{ class Car extends Vehicle{


public String color;
public or protected int wheels; Car(){
super();
Vehicle(){
color = "red";
wheels = 4; }
Car(String color){
} this.color = color;
}
Vehicle(int wheels){ Car (String color, int wheels){
super(wheels);
this.wheels = wheels; this.color = color;
}
}
public void displayParentInfo(){
public int getWheels(){ super.print();
int a = super.wheels;
return wheels; }
}
}

No error
public void setWheels(int wheels){

this.wheels = wheels;

public void print(){

System.out.println("Wheels: "+wheels);

}
SIT Internal

class Vehicle{ class Car extends Vehicle{


public String color;
private int wheels; Car(){
super();
Vehicle(){
color = "red";
wheels = 4; }
Car(String color){
} this.color = color;
}
Vehicle(int wheels){ Car (String color, int wheels){
super(wheels);
this.wheels = wheels; this.color = color;
}
}
public void displayParentInfo(){
public int getWheels(){ super.print();
int a = super.getwheels();
return wheels; }
}
}

public void setWheels(int wheels){

this.wheels = wheels;

public void print(){

System.out.println("Wheels: "+wheels);

}
SIT Internal

class Vehicle{ class Car extends Vehicle{


public String color;
public or protected int wheels; Car(){
super();
Vehicle(){
color = "red";
wheels = 4; }
Car(String color){
} this.color = color;
}
Vehicle(int wheels){ Car (String color, int wheels){
super(wheels);
this.wheels = wheels; this.color = color;
}
}
public void displayParentInfo(){
public int getWheels(){ super.print();
int a = super.wheels;
return wheels; }
}
}

public void setWheels(int wheels){


public static void main(String args[]) {
this.wheels = wheels;
Car cr = new Car("blue",5);
} cr.displayParentInfo();
public void print(){ }
System.out.println("Wheels: "+wheels);

} Output:
}
Wheels: 5
SIT Internal

Visibility Modifiers
Modifier Class Subclass Package World

private

protected

public
SIT Internal

Conclude
• Derive a new class from an existing one allowing classes to inherit properties and behaviors from
other classes.
• It can also extend the functionality of the base class by adding new methods or fields to the child
class.
• It enables the creation of a hierarchy of classes
• Constructors are not inherited, but they are implicitly called
• By using access modifiers, you can control the level of visibility

You might also like