0% found this document useful (0 votes)
10 views13 pages

Inheritance in Java Keywords

Inheritance in Java allows a class to inherit properties and behaviors from another class, promoting code reusability and method overriding for runtime polymorphism. Key concepts include subclasses (child classes), superclasses (parent classes), and access modifiers (private, public, protected, default) that determine how members are inherited. The 'super' keyword is used to access parent class members, while the 'final' keyword prevents overriding of methods and inheritance of classes.

Uploaded by

kartikchauhan190
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)
10 views13 pages

Inheritance in Java Keywords

Inheritance in Java allows a class to inherit properties and behaviors from another class, promoting code reusability and method overriding for runtime polymorphism. Key concepts include subclasses (child classes), superclasses (parent classes), and access modifiers (private, public, protected, default) that determine how members are inherited. The 'super' keyword is used to access parent class members, while the 'final' keyword prevents overriding of methods and inheritance of classes.

Uploaded by

kartikchauhan190
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

INHERITANCE IN JAVA Why use inheritance in java

•For Method Overriding (so runtime polymorphism can be


achieved).
• As the name suggests, inheritance means inheriting •For Code Reusability. Importance of Java inheritance
properties. The mechanism by which a class is allowed Terms used in Inheritance Implementation of inheritance in Java provides
to derive the properties of a different class is termed • Subclass : A subclass, also known as child class or derived
inheritance. With the concept of inheritance, the the following benefits:
class, is the class that inherits the properties and behaviors of •Inheritance minimizes the complexity of a
information in a program can be organized
hierarchically. another class. So, if A and B are two classes and if B class code by minimizing duplicate code. If the same
inherits A class, then the B class is called the subclass. The code has to be used by another class, it can
• Inheritance is one of the critical features of OOPs properties and behavior of a class are the attributes and methods
(Object Oriented Programming System), where a new simply be inherited from that class to its sub-
class is created from an existing class (parent or base
present in the class. So the subclass will get all the attributes class. Hence, the code is better organized.
class). In simple terms, the inheritance would mean and methods of the superclass when it inherits the superclass. •The efficiency of execution of a code
passing down the characteristics of parents(habits, The access modifiers private, public, protected, and default affect increases as the code is organized in a simpler
looks, height) to their child. the way the attributes and methods of the superclass are inherited form.
• The most useful contribution of types of inheritance in
by the subclass. •The concept of polymorphism can be used
java would be the reusability of a code. As discussed • Superclass : A superclass, also known as parent class or base along with inheritance.
earlier that the derived class would inherit the feature, class, is the class whose properties and behaviors are inherited
methods, etc from the parent class so the codes present by the subclass. So, if A and B are two classes and if B class
in the parent class would also be inherited into the inherits A class, then A class is called the superclass. The basic syntax is
derived class, this is how reusability would work.
• Reusability: The technique of reusability allows the user to Class superclass {
• The idea behind inheritance in Java is that you can create a class (a new one) having the fields or methods of an —————-
create new classes that are built upon existing classes. already existing class. It allows reusing the code.
When you inherit from an existing class, you can reuse }
• extends: The keyword “extends” is used while inheriting a
methods and fields of the parent class. Moreover, you
class. It defines that the functionality of the superclass is being class subclass extends superclass
can add new methods and fields in your current class
also. extended to the subclass. {
• Super Keyword :The super keyword is used to refer to the ————–
• Inheritance represents the IS-A relationship which is
also known as a parent-child relationship.
parent class of any class. This keyword can be used to access —————–
the variables, methods, and constructors of the parent class }
• In Java, the concept of inheritance allows the users to from the child class.
create classes and use the properties of existing classes. • Final keyword :If you don't want other classes to inherit from a
class, use the final keyword:
protected Members in Inheritance
In Java, if a class includes protected fields and methods, then these fields and methods are accessible from the subclass of the class. A class’s private members are inaccessible from
outside the class. The private members are only directly accessible to methods of that class. However, as was previously said, it could occasionally be required for a subclass to have
access to a superclass’s private member. Anybody can access a private member if you make it public. In order to block direct access to a member of a superclass outside the class
while yet allowing it to be used in a subclass, you must designate that member protected.

In the given example, we have


created a class named Animal.
The class includes a protected
field: name and a method:
display().

We have inherited the Dog class


that inherits Animal class.
Notice the statement,

[Link] = "Rocky";
[Link]();
Here, we are able to access the
protected field and method of
the superclass using the
labrador object of the subclass.
Super keyword in inheritance
class Superclass
Super keyword usage in inheritance, always refers
{
to its immediate as an object. The super keyword int i =20;
is used to refer to the parent class of any class. void display()
This keyword can be used to access the variables, {
methods, and constructors of the parent class from [Link](“Superclass display method”);
the child class.
}
There are three usages of super keyword in Java: }
class Subclass extends Superclass
1. We can invoke the superclass variables.
{
2. We can invoke the superclass methods. int i = 100;
void display()
3. We can invoke the superclass constructor.
{
[Link]();
[Link](“Subclass display method”);
[Link](“ i value =”+i);
[Link](“superclass i value =”+super.i);
}
}
class SuperUse
{
public static void main(String args[])
{
Subclass obj = new Subclass();
[Link]();
}
}
[Link] program that illustrates access of parent class variable using super keyword
Explanation:

Class TestChildClass inherits the TestParentClass. Both classes have a common


variable, var. In such a conflict, priority is always given to the child class's
variable.
To access the variable of the parent class from the child class, we can make use of
the keyword super.
2. Example program that illustrates access of parent class methods using super keyword.

Explanation:
The class child inherited the parent class, both have a common method test(). In this case, if you try to access test() method from the child class, priority is given to the child class's
test() method.
So, to access the parent test() method from TestChildClass, we can make use of the super keyword.
[Link] program that illustrates access of parent class constructor using super keyword

Explanation:
The super() call inside the child class constructor invokes the constructor of the parent class constructor. The super() class can only be placed as the first statement in the
child class constructor.
Note that the super() call is implicit in java. Any child constructor, when invoked, automatically invokes the parent constructor first, even if no explicitly super() call is
placed in the child class constructor.
In the case of the parent class having a multiple-parameterized constructor, all the parameters that are to be passed to the parent's
constructor should be first passed to the child class constructor. Then the parameters for the parent class should be passed in the super()
call, thereby invoking the parameterized constructor of the parent class. Below example, java program makes this point clear.
Access modifiers and how they affect inheritance

Below is how access modifiers public, private, protected and default affect the way the variables and methods of the parent class are
inherited by the child class
• Private:
Variables and methods declared private in the parent class cannot be inherited by child classes. They can only be accessed in the parent
class. By creating public getter and setter methods, we can access these variables in the child class.

• Public:
Variables and methods declared as public in the parent class would be inherited by the child class and can be accessed directly by the
child class.

• Protected:
Variables and methods declared as protected in the parent class would be inherited by the child class and can be accessed directly by
the child class. But the access level is limited to one subclass. So if the subclass is again inherited by another class, they will not have
access to those protected variables/methods.

• Default:
This is the case where no access modifier is specified. Variables and methods which have default access modifiers in the parent class
can be accessed by the child class.
Java final Method
In Java, the final method cannot be overridden by the child class. For example,

In the above example, we have created a final method


named display() inside the FinalDemo class. Here,
the finalmethod class inherits the FinalDemo class.

We have tried to override the final method in


the finalmethod class. When we run the program, we will get a
compilation error with the following message.
JAVA FINAL KEYWORD in INHERITANCE

In this example, we have created a final class named FinalClass. Here, we have tried to inherit the final class by the Main class.
When we run the program, we will get a compilation error with the following message.
Adding the final keyword in class gives the following error in Eclipse
After removing the final keyword ,the program runs successfully

You might also like