[go: up one dir, main page]

0% found this document useful (0 votes)
6 views11 pages

Inheritance

Inheritance in programming defines the relationship between classes, allowing one class to inherit structure and behavior from another. It includes various types such as single-level, multi-level, hierarchical, multiple, and hybrid inheritance, with the 'extends' keyword used for inheritance. The 'super' keyword is essential for invoking superclass constructors and differentiating between superclass and subclass members.

Uploaded by

pyvbvaraprasad
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)
6 views11 pages

Inheritance

Inheritance in programming defines the relationship between classes, allowing one class to inherit structure and behavior from another. It includes various types such as single-level, multi-level, hierarchical, multiple, and hybrid inheritance, with the 'extends' keyword used for inheritance. The 'super' keyword is essential for invoking superclass constructors and differentiating between superclass and subclass members.

Uploaded by

pyvbvaraprasad
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

Inheritance

Inheritance defines relationship among classes, wherein one class share structure or behavior
defined in one or more classes.
Type
1. Single-level inheritance Inher1.java
2. Multi-level Inheritance
3. Hierarchical Inheritance Inhertest2.java
4. Multiple Inheritance
5. Hybrid Inheritance

 One class inherits the other using extends keyword


 The class being inherited from is called the parent class or superclass. The class that is
inheriting is called the child class or subclass.
 A subclass can access the attributes and methods of its superclass (if not private).
 Multilevel inheritance but no multiple inheritance
 There is a special way to call the superclass’s constructor
 There is automatic dynamic method dispatch
 Inheritance provides code reusability (code of any class can be used by extending that class)
IS-A Relationship
• In inheritance, subclass and superclass follow the ‘is-a’ relationship.
• Derived class instance can access all the properties of base class
Simple inheritance
Java Subclass with Constructor
The constructor of a subclass can call the constructor of its superclass, but the reverse
is not true.
Let’s see how the constructor of a superclass is called from the constructor of its
subclass.
The creation and initialization of the superclass object is a prerequisite to the
creation of the subclass object.
When a subclass object is created,
• It creates the superclass object
• Invokes the relevant superclass constructor.
• The initialized superclass attributes are then inherited by the subclass object

finally followed by the creation of the subclass object


• initialization of its own attributes through a relevant constructor
subclass
• The constructors of the superclass are never inherited by the subclass
• This is the only exception to the rule that a subclass inherits all the properties of
its superclass
Inhertest2.java
super
 super keyword is similar to this keyword.
 It is used to differentiate the members of superclass from the members of subclass, if they
have same names.
 It is used to invoke the superclass constructor from subclass.
 ‘super’ is a keyword used for variables of super class from sub class (super.a=a;)
 It is used to call a constructor of super class from constructor of sub class which should be
first statement.(super(a,b);)
 It is used to call a super class method from sub class method to avoid redundancy of code
(super.addNumbers(a, b)) Inher3.java
Using super to Call Superclass Constructors
• super() if present, must always be the first statement executed inside a
subclass constructor.
• It clearly tells you the order of invocation of constructor in a class
hierarchy.
• Constructors are invoked in the order of their derivation
Inhertest3.java
Multilevel Inheritance Inher6.java

You might also like