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

Inheritance First Part

The document discusses inheritance in Java, where a subclass can inherit attributes and methods from a superclass. A subclass extends a superclass using the extends keyword. The document provides an example where the Car subclass inherits from the Vehicle superclass, allowing a Car object to access attributes and methods from Vehicle. The document also discusses protected access modifiers and reusability through inheritance.

Uploaded by

tayyabchishty005
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)
27 views

Inheritance First Part

The document discusses inheritance in Java, where a subclass can inherit attributes and methods from a superclass. A subclass extends a superclass using the extends keyword. The document provides an example where the Car subclass inherits from the Vehicle superclass, allowing a Car object to access attributes and methods from Vehicle. The document also discusses protected access modifiers and reusability through inheritance.

Uploaded by

tayyabchishty005
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/ 9

Object Oriented Programming

Inheritance in Java

In Java, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:

 subclass (child) - the class that inherits from another class


 superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

In the example below, the Car class (subclass) inherits the attributes and methods from
the Vehicle class (superclass):

Example

class Vehicle {

protected String brand = "Ford"; // Vehicle attribute

public void honk() { // Vehicle method

System.out.println("Tuut, tuut!");

}
class Car extends Vehicle {

private String modelName = "Mustang"; // Car attribute

public static void main(String[] args) {

// Create a myCar object

Car myCar = new Car();

// Call the honk() method (from the Vehicle class) on the myCar object

myCar.honk();

// Display the value of the brand attribute (from the Vehicle class) and the value of the
modelName from the Car class

System.out.println(myCar.brand + " " + myCar.modelName);

}
Did you notice the protected modifier in Vehicle?

We set the brand attribute in Vehicle to a protected access modifier. If it was set to private, the
Car class would not be able to access it.

Further details
Inheritance can be defined as the process where one class acquires the properties (methods and
fields) of another. With the use of inheritance the information is made manageable in a
hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class)
and the class whose properties are inherited is known as superclass (base class, parent class).
extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of
extends keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
Sample Code
Following is an example demonstrating Java inheritance. In this example, you can observe two
classes namely Calculation and My_Calculation.
Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of
Calculation class.
Copy and paste the following program in a file with name My_Calculation.java
Example

class Calculation {
int z;

public void addition(int x, int y) {


z = x + y;
System.out.println("The sum of the given numbers:"+z);
}

public void Subtraction(int x, int y) {


z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}

public class My_Calculation extends Calculation {


public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}

public static void main(String args[]) {


int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
After executing the program, it will produce the following result −
Output
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
In the given program, when an object with the name demo of My_Calculation class is created, a
copy of the contents of the superclass / Calculation is made within it. That is why, using the
object(demo) of the subclass(child class) / My_Calculation you can access the members of a
superclass(parent class) / Calculation .
The Superclass reference variable can hold the subclass object, but using that variable you can
access only the members of the superclass( it means if we create object of parent class it can
only access the variables and methods of parent class not of child class), so to access the
members of both classes it is recommended to always create reference variable to the subclass.
If you consider the above program, you can instantiate the class as given below. But using the
superclass reference variable ( cal in this case) you cannot call the method multiplication(),
which belongs to the subclass My_Calculation.
Calculation demo = new Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);

The syntax of Java Inheritance

1. class Subclass-name extends Superclass-name


2. {
3. //methods and fields
4. }

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.

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);
}
}
Test it Now

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.

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.

Ye video both part ko cover karey gi so half video is for this part and next half is for next part of
inheritance…..

Video Lecture Link


https://www.youtube.com/watch?v=qG4zIEjYz7I

You might also like