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

JAVA INHERITANCE NOTES

Inheritance in Java allows one class to inherit properties and behaviors from another, promoting code reusability and method overriding. There are five types of inheritance: single, multilevel, hierarchical, multiple (achieved through interfaces), and hybrid. The document provides examples and syntax for each type, illustrating how inheritance is implemented in Java.

Uploaded by

Alex Nyabuto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

JAVA INHERITANCE NOTES

Inheritance in Java allows one class to inherit properties and behaviors from another, promoting code reusability and method overriding. There are five types of inheritance: single, multilevel, hierarchical, multiple (achieved through interfaces), and hybrid. The document provides examples and syntax for each type, illustrating how inheritance is implemented in Java.

Uploaded by

Alex Nyabuto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented programming
system).

The idea behind inheritance in Java is that you can create new classes that are built upon existing
classes. When you inherit from an existing class, you can reuse methods and fields of the parent
class. Moreover, you can add new methods and fields in your current class also.

Why use inheritance in java

 For Method Overriding (so runtime polymorphism can be achieved).


 For Code Reusability.

Terms used in Inheritance

 Class: A class is a group of objects which have common properties. It is a template or


blueprint from which objects are created.
 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.
 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.
 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

1. class Subclass-name extends Superclass-name {


2. //methods and fields
3. }

 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.

Types of inheritance
There are five types of inheritance.

1. Single Inheritance

In single inheritance, a single subclass extends from a single superclass. For example,
Example Code

// Java program to illustrate the

// concept of single inheritance

import java.io.*;

import java.lang.*;

import java.util.*;

// Parent class

class One {

public void print_geek()

System.out.println("Geeks");

class Two extends One {

public void print_for() { System.out.println("for"); }

}
// Driver class

public class Main {

// Main function

public static void main(String[] args)

Two g = new Two();

g.print_geek();

g.print_for();

g.print_geek();

2. Multilevel Inheritance

In multilevel inheritance, a subclass extends from a superclass and then the same subclass acts as
a superclass for another class. For example,
Code Example

// Importing required libraries

import java.io.*;

import java.lang.*;

import java.util.*;

// Parent class One

class One {

// Method to print "Geeks"

public void print_geek() {

System.out.println("Geeks");

// Child class Two inherits from class One

class Two extends One {

// Method to print "for"

public void print_for() {

System.out.println("for");

// Child class Three inherits from class Two

class Three extends Two {

// Method to print "Geeks"

public void print_lastgeek() {


System.out.println("Geeks");

// Driver class

public class Main {

public static void main(String[] args) {

// Creating an object of class Three

Three g = new Three();

// Calling method from class One

g.print_geek();

// Calling method from class Two

g.print_for();

// Calling method from class Three

g.print_lastgeek();

3. Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses extend from a single superclass. For example,
Code Example
// Java program to illustrate the
// concept of Hierarchical inheritance

class A {
public void print_A() { System.out.println("Class A"); }
}

class B extends A {
public void print_B() { System.out.println("Class B"); }
}

class C extends A {
public void print_C() { System.out.println("Class C"); }
}

class D extends A {
public void print_D() { System.out.println("Class D"); }
}
// Driver Class
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();

C obj_C = new C();


obj_C.print_A();
obj_C.print_C();

D obj_D = new D();


obj_D.print_A();
obj_D.print_D();
}
}

4. Multiple Inheritance

In multiple inheritance, a single subclass extends from multiple superclasses. For example,
Note: Java doesn't support multiple inheritance. However, we can achieve multiple inheritance using
interfaces. To learn more, visit Java implements multiple inheritance.
Code example
// Java program to illustrate the
// concept of Multiple inheritance
import java.io.*;
import java.lang.*;
import java.util.*;

interface One {
public void print_geek();
}

interface Two {
public void print_for();
}

interface Three extends One, Two {


public void print_geek();
}
class Child implements Three {
@Override public void print_geek()
{
System.out.println("Geeks");
}

public void print_for() { System.out.println("for"); }


}
// Drived class
public class Main {
public static void main(String[] args)
{
Child c = new Child();
c.print_geek();
c.print_for();
c.print_geek();
}
}

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. For example,

Code example
public class SolarSystem {
}
public class Earth extends SolarSystem {
}
public class Mars extends SolarSystem {
}
public class Moon extends Earth {
}
MORE CODE EXAMPLES ON IHERITENCE
Example 1
package examples;
class Vehicle {
protected String brand;
String modelName;
public void honk(){
System.out.println("tuut ,tuut");
}
}
class Car extends Vehicle {
int price(int x) {
return x;
}
public static void main(String[] args){
Car c = new Car();
c.modelName = "Toyota";
c.brand = "Fielder";

Car p = new Car();

p.modelName = "Mazda";
p.brand = "CX5";

System.out.print("My car model produces the sound> ");


c.honk();
System.out.println("My car brand name is a "+c.brand+"
and the model is: "+c.modelName);

System.out.print("My car model produces the sound> ");


p.honk();
System.out.println("My car price is> "+p.price(788888));
System.out.println("My car brand name is a "+p.brand+"
and the model is: "+p.modelName);
}
}
Example 2
package examples;

class Mama {
public void motherMoney(){
System.out.println("Mama's money");
}
}
class Linet extends Mama{
public void urefu(){
System.out.println("Linet ni mrefu saana");}
}

class John extends Mama {


public void ufupi(){
System.out.println("John ni mfupi\n");
}
}

class TestInheritance{
public static void main(String args[]){
John j =new John();

System.out.println("Below are the details of


John!\n");
j.motherMoney();
j.ufupi();
//j.urefu();

System.out.println("Below are the details of


Linet!\n");
Linet L = new Linet();
L.motherMoney();
L.urefu();
//L.ufupi();

}
}
Example 3
package groupwork;
public class Man {
//These are the fields for a man
String name;
int age;
String village;

//Has a method walk


public void walk() {
System.out.println("The man walks");
}
}
//Child class Dennis
class Dennis extends Man{

//Main method
public static void main(String[] args) {
//First Man
Dennis deno = new Dennis();

deno.name = "Shadrack Kiplangat";


deno.age= 17;
deno.village = "Keroka Village";

System.out.println("The first man's name is


"+deno.name);
System.out.println("The first man's age is
"+deno.age);
System.out.println("He comes from
"+deno.village);
deno.walk();

//Second man
Dennis shaddy = new Dennis();

shaddy.name = "Alex Ogendo";


shaddy.age= 27;
shaddy.village = "Kisii Village";

System.out.println("\nThe second man's name is


"+shaddy.name);
System.out.println("The second man's age is
"+shaddy.age);
System.out.println("He comes from
"+shaddy.village);
shaddy.walk();
}

You might also like