Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Inheritance in Java

The document explains inheritance in Java, a key concept in Object-Oriented Programming that allows one class to inherit features from another. It covers the benefits of inheritance such as code reusability, method overriding, and abstraction, and details the use of the 'extends' keyword. Additionally, it outlines different types of inheritance supported by Java, including single, multilevel, hierarchical, multiple, and hybrid inheritance, along with example programs for each type.

Uploaded by

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

Inheritance in Java

The document explains inheritance in Java, a key concept in Object-Oriented Programming that allows one class to inherit features from another. It covers the benefits of inheritance such as code reusability, method overriding, and abstraction, and details the use of the 'extends' keyword. Additionally, it outlines different types of inheritance supported by Java, including single, multilevel, hierarchical, multiple, and hybrid inheritance, along with example programs for each type.

Uploaded by

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

Week 2 INHERITANCE

Oop java

Syed Arslan
JINNAH SCIENCE COLLEGE FAQIRWALI
Inheritance in Java
Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the
mechanism in Java by which one class is allowed to inherit the features(fields and
methods) of another class. In Java, Inheritance means creating new classes based on
existing ones. A class that inherits from another class can reuse the methods and
fields of that class. In addition, you can add new fields and methods to your current
class as well.
Why Do We Need Java Inheritance?
• Code Reusability: The code written in the Superclass is common to all
subclasses. Child classes can directly use the parent class code.
• Method Overriding: Method Overriding is achievable only through
Inheritance. It is one of the ways by which Java achieves Run Time
Polymorphism.
• Abstraction: The concept of abstract where we do not have to provide all
details, is achieved through inheritance. Abstraction only shows the
functionality to the user.
Super Class/Parent Class:
The class whose features are inherited is known as a superclass (or a base class or a
parent class).
Sub Class/Child Class:
The class that inherits the other class is known as a subclass (or a derived class,
extended class, or child class). The subclass can add its own fields and methods in
addition to the superclass fields and methods.
How to Use Inheritance in Java?
The extends keyword is used for inheritance in Java. Using the extends keyword
indicates you are derived from an existing class. In other words, “extends” refers to
increased functionality.
Syntax :
class Child-Class extends Parent-Class
{
//methods and fields
}
Inheritance in Java Example
Example: In the below example of inheritance, class Bicycle is a base class, class
MountainBike is a derived class that extends the Bicycle class and class Test is a
driver class to run the program.

// Base class
class Bicycle {
// Fields (attributes)
public int gear;
public int speed;
// Constructor
public Bicycle(int gear, int speed) {
this.gear = gear;
this.speed = speed;
}
public void displayBicycleDetails() { / Method to display the
bicycle's details
System.out.println("Gear: " + gear + ", Speed: " + speed);
}
}
class MountainBike extends Bicycle { // Derived class
// Additional field for the MountainBike class
public int seatHeight;
// Constructor
public MountainBike(int gear, int speed, int seatHeight) {
// Call the constructor of the base class
super(gear, speed);
this.seatHeight = seatHeight;
}
// Method to display the mountain bike's details
public void displayMountainBikeDetails() {
System.out.println("Gear: " + gear + ", Speed: " + speed + ", Seat Height: " +
seatHeight);
}
}
// Driver class
public class Test {
public static void main(String[] args) {
// Create an object of the Bicycle class
Bicycle bicycle = new Bicycle(5, 20);
System.out.println("Bicycle Details:");
bicycle.displayBicycleDetails();

// Create an object of the MountainBike class


MountainBike mountainBike = new MountainBike(3, 15, 10);
System.out.println("MountainBike Details:");
mountainBike.displayMountainBikeDetails();
}
}

Java Inheritance Types :

Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the
properties and behavior of a single-parent class. Sometimes, it is also known as simple
inheritance.
class Animal {
public String name; // Field (attribute)
public Animal(String name) { // Constructor
this.name = name;
}
public void displayAnimalDetails() { // Method to display the
animal's name
System.out.println("Animal Name: " + name);
}
}
class Dog extends Animal { // Derived class
public Dog(String name) { // Parameterized constructor
this.name = name; // Directly assigning the name
}
public void displayDogDetails() { // Method to display the dog's name
System.out.println("Dog Name: " + name);
}}
public class Test {
public static void main(String[] args) {
Animal animal = new Animal("Generic Animal"); // Create an
object of the Animal
System.out.println("Animal Details:");
animal.displayAnimalDetails();
Dog dog = new Dog("Buddy"); // Create an object of the Dog
class
System.out.println("Dog Details:");
dog.displayDogDetails();
}
}
Multilevel Inheritance:
Multilevel inheritance in Java is a type of inheritance where a class is derived from
another class, which is also derived from another class. In other words, it forms a chain
of inheritance. This is useful when you want to create a hierarchy of classes that build
on one another, inheriting properties and behaviors from their parent classes.

Inheritance Programs
Program 1: Single Inheritance (Car and Engine)
This program demonstrates single inheritance where the Engine class is inherited by the
Car class. The Car class contains an instance of Engine, showing the composition
relationship.

class Engine {
String type;
Engine(String type) {
this.type = type;
}
void showType() {
System.out.println("Engine type: " + type);
}}
class Car extends Engine {
String model;
Car(String type, String model) {
super(type);
this.model = model;
}
void showDetails() {
showType();
System.out.println("Car model: " + model);
} }
public class Main {
public static void main(String[] args) {
Car car = new Car("V8", "Mustang");
car.showDetails();
}
}

Program 2: Single Inheritance (Person and Employee)


This program shows single inheritance where the Employee class inherits from the
Person class. The Employee class has additional attributes specific to employees.

class Person {
String name;
Person(String name) {
this.name = name;
}
void showName() {
System.out.println("Name: " + name);
} }

class Employee extends Person {


int employeeId;
Employee(String name, int employeeId) {
this.name = name;
this.employeeId = employeeId;
}
void showDetails() {
showName();
System.out.println("Employee ID: " + employeeId);
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("Alice", 101);
emp.showDetails();
}
}

Program 3: Multilevel Inheritance (Animal, Mammal, Dog)


This program demonstrates multilevel inheritance where the Dog class inherits from
Mammal, which in turn inherits from Animal. It shows the hierarchical relationship.

class Animal {
String species;
Animal(String species) {
this.species = species;

void showSpecies() {
System.out.println("Species: " + species);
}
}
class Mammal extends Animal {
int legs;
Mammal(String species, int legs) {
this.species = species;
this.legs = legs;
}
void showDetails() {
showSpecies();
System.out.println("Number of legs: " + legs);
}
}
class Dog extends Mammal {
String breed;
Dog(String species, int legs, String breed) {
this.species = species;
this.legs = legs;
this.breed = breed;

void showAllDetails() {
showDetails();
System.out.println("Breed: " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Canine", 4, "Labrador");
dog.showAllDetails();
}
}
Program 4: Single Inheritance (Book and E Book)
This program demonstrates single inheritance where the EBook class inherits from the
Book class, adding an additional attribute for the file size.

class Book {
String title;
Book(String title) {
this.title = title;
}
void showTitle() {
System.out.println("Title: " + title);
}
}
class EBook extends Book {
double fileSize;
EBook(String title, double fileSize) {
this.title = title;
this.fileSize = fileSize;
}

void showDetails() {
showTitle();
System.out.println("File size: " + fileSize + "MB");
}
}
public class Main {
public static void main(String[] args) {
EBook ebook = new EBook("Java Programming", 2.5);
ebook.showDetails();
}
}

Program 5: Multilevel Inheritance (Device, Computer, Laptop)


This program demonstrates multilevel inheritance where the Laptop class inherits from
Computer, which in turn inherits from Device.

class Device {
String brand;
Device(String brand) {
this.brand = brand;
}
void showBrand() {
System.out.println("Brand: " + brand);
}
}
class Computer extends Device {
int ram;
Computer(String brand, int ram) {
this.brand = brand;
this.ram = ram;
}
void showDetails() {
showBrand();
System.out.println("RAM: " + ram + "GB");
}
}
class Laptop extends Computer {
double weight;
Laptop(String brand, int ram, double weight) {
this.brand = brand;
this.ram = ram;
this.weight = weight;
}
void showAllDetails() {
showDetails();
System.out.println("Weight: " + weight + "kg");
}
}
public class Main {
public static void main(String[] args) {
Laptop laptop = new Laptop("Dell", 16, 1.5);
laptop.showAllDetails();
}
}

You might also like