[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]
[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1581440267965{background-color: #f6f6f6 !important;}”]
Inheritance in Java is a powerful way to reuse code from existing classes. In Java, we can derive classes from other classes, thereby inheriting fields and methods from those classes.
[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
It’s prevalent that you want to reuse code from classes you have already programmed. Instead of coding similar classes several times, it’s possible to inherit code from another class. A class that inherits code is called a subclass, and its parent is a superclass.
Furthermore, what is comfortable with inheritance is that the subclass gets the same set of instance variables and methods as the superclass. Still, it is also possible to add new methods and class variables if desired.
To summarize, Interface in Java is
[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
Inheritance is created with the command extend. The syntax for creating a subclass is:
class subclassName extends superclassName{
super(data-type1 name1, data-type2 name2 ) // and so on..
}
[/vc_column_text][vc_empty_space height=”15px”][vc_column_text]
There are a couple of different types of inheritance in Java that are good to know and hopefully increase the understanding of how inheritance works.
Note how we use the word extends for a class to inherit code.[/vc_column_text][vc_empty_space height=”30px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_column_text]
class A {
// ....
}
class B extends A {
// ....
}
[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_empty_space height=”30px”][vc_single_image image=”20047″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”30px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_column_text]
class A {
// ....
}
class B extends A {
// ....
}
class C extends B {
// ....
}
[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_single_image image=”20049″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”30px”][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_column_text]
class A {
// ....
}
class B extends A {
// ....
}
class C extends A {
// ....
}
[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_empty_space height=”100px”][vc_single_image image=”20050″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”50px”][vc_column_text]Important to note is that Java does not support so-called multiple inheritances as one class inherits code from multiple classes. E.g., that class C inherits code from A and B at the same time. This means that one class cannot extend to more than one class. However, a class can implement one or more so-called Interface, which we will discuss on the next page.
The image below shows an example of a multiple inheritance[/vc_column_text][vc_empty_space height=”20px”][vc_single_image image=”20051″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][vc_empty_space height=”30px”][vc_column_text]
Let’s continue with the example from the previous page, the programming of a dog. However, note that the we have created a subclassed (highlighted lines) named “superDog”.
class Dog {
// Defines the Attributes of the dog class
String name;
int age;
String breed;
String color;
// Define the constructor
Dog(String name, int age, String breed, String color){
this.name = name;
this.age = age;
this.breed = breed;
this.color = color;
}
// Defines the methods for the dog class
void bark(){
Sytem.out.println(this.name + " is barking ");
}
void eat(){
Sytem.out.println(this.name + " is eating ");
}
void sleep(){
Sytem.out.println(this.name + " is ZZzzZZzzz ");
}
public static void main(String args[]){
Dog dog = new Dog("Brady", 3, "Terrier", "Brown");
Dog dog2 = new Dog("Steven", 5, "Pudel", "Black");
}
SuperDog superDog = new SuperDog("Bolt", 2, "German shepherd", "White");
dog.sleep();
dog2.sleep();
newDog.sleep();
}
}
class SuperDog extends Dog {
SuperDog(String name, int age, String breed, String color) {
super(name, age, breed, color);
}
void sleep(){
System.out.println(this.name + " is not tired and not going to sleep!");
}
}
Let’s start by having a look at the subclass in (lines 50-59):
class SuperDog extends Dog {
SuperDog(String name, int age, String breed, String color) {
super(name, age, breed, color);
}
void sleep(){
System.out.println(this.name + " is not tired and not going to sleep!");
}
}
If we let each object call the “sleep ()” method (lines 42-44), the printout will be:
Brady is ZZzzZZzzz Steven is ZZzzZZzzz Bolt is not tired and not going to sleep!
Of course, the other methods of the superclass are also there in the subclass. The command:
superDog.eat();
gives the result:
Bolt is eating
[/vc_column_text][vc_empty_space height=”15px”][vc_column_text]
Furthermore, if you want to add class variables and new methods, you can easily add them to the subclass:
class SuperDog extends Dog {
public int velocity;
SuperDog(String name, int age, String breed, String color, int velocity) {
super(name, age, breed, color);
this.velocity = velocity;
}
void sleep(){
System.out.println(this.name + ": is not tired and not going to sleep!");
}
void run(){
System.out.println(this.name + ": running at speed " + getVelocity() + "km/h");
}
int getVelocity(){
return this.velocity;
}
}
[/vc_column_text][vc_empty_space height=”15px”][vc_empty_space height=”15px”][vc_column_text]Now five input parameters are required. We now create an object from the sub-class “SuperDog” as:
SuperDog superDog = new SuperDog("Bolt", 2, "German shepherd", "white", 10);
And then using the method run() by
superDog.run();
We get the following result:
Bolt: running at speed 10 km/h
[/vc_column_text][vc_empty_space][vc_column_text]
[/vc_column_text][vc_empty_space][/vc_column][/vc_row]