0% found this document useful (0 votes)
12 views8 pages

Understanding Java Inheritance Basics

Inheritance in Java allows a class to inherit properties and methods from another class, promoting code reusability and enabling polymorphism. There are different types of inheritance, including single, multilevel, and hierarchical inheritance, but multiple inheritance is not supported. The document outlines the syntax, access specifiers, and examples for each type of inheritance in Java.

Uploaded by

hacker76849
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)
12 views8 pages

Understanding Java Inheritance Basics

Inheritance in Java allows a class to inherit properties and methods from another class, promoting code reusability and enabling polymorphism. There are different types of inheritance, including single, multilevel, and hierarchical inheritance, but multiple inheritance is not supported. The document outlines the syntax, access specifiers, and examples for each type of inheritance in Java.

Uploaded by

hacker76849
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

INHERITANCE :- Inheritance is a process in which a class give it’s properties to another class.

 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 classes , you can reuse methods and fields
of the parent class. Moreover , you can add new methods and fields in your currents class also.
 Inheritance represents the IS-A relationship which is also known as parent-child relationship.
 The existing is class called : parent-class or super-class or Base-class .
 The new class is called : Child-class or Sub-class or Derived-class .
 The keyword used for inheritance is extends.
 The extends keyword extends a class (indicates that a class is inherited from anther class) .It
is possible to inherit attributes and methods from one class to another by using

Why we use inheritance in java ?


• For code Reusability.
(The code that is present in the parent class can be directly used by the child class ).
Hence , we can achieve polymorphism in java with the help of inheritance.
SYNTAX OF JAVA INHERITANCE
class subclass-name extends superclass-name
{
// methods and statements
}

TYPES OF INHERITANCE

 Note
 Multiple inheritance and hybrid inheritance are not allowed in java
using class concept.
SL NO. BASE CLASS CAN BE ACCESSED CAN BE ACCESSED
MEMBER SPECIFIER BY DERIVED BY DERIVE CLASS
CLASS OR OBJECT ?
MEMBER ?
1 Public YES YES
2 default YES YES
3 private NO NO
4 protected YES YES

NOTE :
• In the absence of any other explicit super-class ,every class is implicity a
subclass of object class.
•A super class can have any number of sub-classes .
•A class can have only one super-class because java doesn’t support multiple
inheritance.
• Except object class , which has no super-class ,every class has one and only one
direct super-class.
•Constructor(s) of base class are not inherited. But the public and protected
member function of base class can access the private members of the base –class.
SINGLE INHERITANCE :- EXAMPLE :-
import [Link].*;
When a class inherits another class class one
{
it is known as single inheritance . int a;
Scanner sc=new Scanner([Link]);
Structure : parent class Void assign1()
{
[Link](“enter a value:”);
a=[Link]();
child class [Link](“value of a is:”+a);
}
Syntax : class parentClass }
class two extends one
{ {
// parentClass definition int b;
Scanner sc=new Scanner([Link]);
} {
void assign2()

class childClass extends [Link](“enter a value:”);


b=[Link]();
parentClass [Link](“value of b is:”+b);
}
{ }
Class inheritance
// childClass definition {
public static void main(String ar[])
} {
two obj=new two();
obj.assign1();
obj.assign2();
}
MULTILEVEL INHERITANCE :- EXAMPLE :-
When there is a chain of inheritance , import [Link].*;
class one
it is known as multilevel inheritance. {
int a;
Structure : parent class Scanner sc=new Scanner([Link]);
void assign1() {
[Link](“enter a number:”);
Intermediate parent class a=[Link]();
[Link](“value of a is:”+a);
Child class }
}
class two extends one
Syntax : class parentClass {
int b;
{
Scanner sc=new Scanner([Link]);
// parentClass definition void assign2() {
} [Link](“enter a number:”);
b=[Link]();
class intermediateClass [Link](“value of b is:”+b);
extends parentClass }
}
{
class three extends two
// intermediateClass definition {
} int c;
Scanner sc=new Scanner([Link]);
class childClass void assign3() {
{ [Link](“enter a number:”);
c=[Link]();
// childClass definition
[Link](“value of C is:”+c);
} }
}
class inheritance2
{
public static void main(String ar[])
{
three obj=new three();
obj.assign1();
obj.assign2();
obj.assign3();
HIERARCHICAL INHERITANCE :- EXAMPLE :-
import [Link].*;
When two or more classes inherits a single class, class one
it is known as hierarchical inheritance. {
int a;
Structure : parent class Scanner sc=new Scanner([Link]);
void assign1() {
[Link](“enter a number:”);
child class child class a=[Link]();
Syntax :-class parentClass [Link](“value of a is:”+a);
}
{ }
class two extends one
// parentClass definition {
} int b;
Scanner sc=new Scanner([Link]);
class childClass void assign2() {
{ [Link](“enter a number:”);
b=[Link]();
// ChildClass definition [Link](“value of b is:”+b);
}
} }
class childClass2 extends class three extends one {
int c;
parentClass Scanner sc=new Scanner([Link]);
{ void assign3() {
[Link](“enter a number:”);
// childClass definition c=[Link]();
} [Link](“value of c is:”+c);
}
}
class inheritance3
{
public static void main(String ar[])
{
two obj=new two();
obj.assign1();
obj.assign2();
three ok=new three();
ok.assign3();
}

Common questions

Powered by AI

Single inheritance in Java occurs when a class inherits directly from another class, such as class 'two' extending class 'one'. Multilevel inheritance involves a chain of inheritance where a class extends from another class that itself is a subclass, such as class 'three' extending 'two', which extends 'one' .

The primary purpose of using inheritance in Java is to enable code reusability. By inheriting from an existing class, the subclass can reuse the methods and fields of the parent class. This means that common functionalities need not be rewritten in every new class, thereby reducing redundancy and improving maintainability .

Java does not support multiple inheritance using classes to avoid complexity and ambiguity arising from the Diamond Problem, where a subclass inherits from multiple classes with the same method signatures. Instead, Java offers interfaces to allow multiple inheritance of method specifications without associated field or method body inheritance .

The 'extends' keyword in Java establishes inheritance between classes, signifying that a subclass is a derivative of a superclass. This enables the subclass to inherit the methods and fields of the superclass, thereby declaring an IS-A relationship, which is fundamental to object-oriented class hierarchies .

Java supports the IS-A relationship through inheritance by allowing a subclass to inherit properties and behaviors from a superclass. This forms a parent-child relationship where the subclass is considered a specialized version of its superclass, indicating that it 'is a' type of the parent class .

In Java, constructors are not inherited, meaning subclass constructors do not automatically invoke superclass constructors unless explicitly done so using the 'super()' method. This encourages clear initialization logic within the class hierarchy, as each class must define how it intends to construct its instances, allowing for controlled constructor chaining .

In Java, public and protected members of a superclass can be accessed by its subclass, while private members cannot be directly accessed. This limitation requires careful design to ensure that necessary methods and fields are accessible while maintaining encapsulation. This impacts object-oriented design by necessitating a balance between flexibility and security when setting access levels .

Access modifiers in Java, such as public, protected, and private, determine the accessibility of class members. Public and protected members are inherited and can be accessed by subclasses, while private members are not directly accessible. This control over member visibility promotes encapsulation by restricting access to sensitive data and behaviours, maintaining a controlled interface with other classes .

Hierarchical inheritance occurs when two or more classes inherit from the same parent class. It differs from single and multilevel inheritance by allowing multiple subclasses to share the same superclass, exemplified in Java with classes 'two' and 'three' both extending class 'one' .

In Java, the Object class is the root of the class hierarchy, serving as the superclass for all other classes. Its significance lies in providing fundamental methods like 'equals()', 'hashCode()', and 'toString()' that are inherited by all Java objects, ensuring a standard method structure across diverse classes .

You might also like