COVID -19 #Stay Home Stay Safe
Chapter-4
Inheritance
Java में Inherita nce ये Object Oriented Programming का एक प्रकार है | जिसमे एक class की
properties और methods ककसी द ुस रे class में inherit की िाती है |
Inheritance में मु ख्यतः Parent class और child class का इस्ते माल ककया िाता है | इसमे Parent
class को Base class या super class भी कहा िाता है और Child class को Derived class या sub
class भी कहा िाता है | C++ ये Inheritance के प्रकार को support करता है , ले ककन Java; Multiple
Inheritance को support नह ीं करता मतलब Java में Parent class को कई child classes हो सकते
है , ले ककन child classes को ससर्फ एक ह Parent class होता है |
Simple Inheritance
Java में Inheritance के सलए extends keyword का इस्ते माल ककया िाता है |
Syntax
class parent_class{
//statements;
class child_class extends parent_class{
//statements;
Example
यहााँ example में A ये एक parent class है और B ये child class है | B class; A class की सभी
properties; inherit कर सकता है |
class A{
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
//statements;
class B extends A{
//statements;
Full Example for Inheritance
Source Code :
//[Link]
class A{
void disp(){
[Link]("Parent Class");
class B extends A{
void show(){
[Link]("Child Class");
public static void main(String args[]){
B obj = new B();
[Link]();
[Link]();
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
Output :
Parent Class
Child Class
Access Modifiers:-
Access Modifiers; Classes, Data members, Methods और Constrctutor के accessbility के
सलए इस्ते माल ककया िाता है |
Access Modifiers चार प्रकार के होते है |
1. default Access Modifier
2. private Access Modifier
3. public Access Modifier
4. protected Access Modifier
1. default Access Modifier
अगर कोई Modifier नह ीं ददया िाता तो, bydefault default ददया िाता है |
default Modifier ससर्फ packa ge के अन्दर ह accessible होता है |
Program में दे खे तो दो packages बनाये गए है | class A को ककसी package से access ककया
िाता है | ले ककन defa ult Modifier होने के कारण ये access नह ीं होता |
//[Link]
package pack1;
class A{
void disp(){
[Link]("class A");
//[Link]
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
package pack2;
import pack1.*;
class B{
public static void main(String args[]){
A a = new A();
[Link]();
Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac -d . [Link]
C:\Program Files\Java\jdk1.8.0_111\bin>javac -d . [Link]
[Link]: error: A is not public in pack1; cannot be accessed from outside package
A a = new A();
[Link]: error: A is not public in pack1; cannot be accessed from outside package
A a = new A();
2 errors
2. private Access Modifier
private Access Modifier ये ससर्फ class के अन्दर accessible होते है |
Program में दे खे तो parent class में a और b नाम के दो instance variables सलए है | ले ककन a
variable; private है | ये variable ससर्फ अपने class के अन्दर ह accessible होते है | Program
में b variable द ुस रे Child class से access हो रहा है , ले ककन a variable access नह ीं हो रहा |
Source Code :
class Parent{
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
private int a = 5;
int b = 10;
class Child extends Parent{
void disp(){
[Link]("Value of a : " + a);
[Link]("Value of b : " + b);
public static void main(String args[]){
Child c = new Child();
[Link]();
Output :
[Link]: error: a has private access in Parent
[Link]("Value of a : " + a);
3. public Access Modifier
public Access Modifier से कहा पर भी access ककया िा सकता है |
//[Link]
package pack1;
public class A{
public void disp(){
[Link]("class A");
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
//[Link]
package pack2;
import pack1.*;
class B{
public static void main(String args[]){
A a = new A();
[Link]();
Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac -d . [Link]
C:\Program Files\Java\jdk1.8.0_111\bin>javac -d . [Link]
C:\Program Files\Java\jdk1.8.0_111\bin>java pack2.B
class A
4. protected Access Modifier
ये ससर्फ अपने packa ge के अन्दर और अपने sub-class पर accessible होते है |
Source Code :
class Parent{
int a = 5;
int b = 10;
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
protected void disp(){
[Link]("Value of a : " + a);
[Link]("Value of b : " + b);
class Child extends Parent{
public static void main(String args[]){
Child c = new Child();
[Link]();
Output :
Value of a : 5
Value of b : 10
Accessing protected method Outside Package
//[Link]
package pack1;
public class A{
protected void disp(){
[Link]("class A");
//[Link]
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
package pack2;
import pack1.*;
class B extends A{
public static void main(String args[]){
B b = new B();
[Link]();
Output :
C:\Program Files\Java\jdk1.8.0_111\bin>javac -d . [Link]
C:\Program Files\Java\jdk1.8.0_111\bin>javac -d . [Link]
C:\Program Files\Java\jdk1.8.0_111\bin>java pack2.B
class A
Constructor Chaining :-
Calling a constructor from the another constructor of same class is known as
Constructor chaining. The real purpose of Constructor Chaining is that you can pass
parameters through a bunch of different constructors, but only have the initialization
done in a single place. This allows you to maintain your initializations from a single
location, while providing multiple constructors to the user. If we don’t chain, and two
different constructors require a specific parameter, you will have to initialize that
parameter twice, and when the initialization changes, you’ll have to change it in every
constructor, instead of just the one.
As a rule, constructors with fewer arguments should call those with more
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
Let’s understand this with the help of an example.
Constructor Chaining Example
In this example, I have several constructors, one constructor is calling another
constructor using this keyword.
this() should always be the first statement in constructor otherwise you will
get this error message: Exception in thread “main” [Link]:
Unresolved compilation problem: Constructor call must be the first
statement in a constructor
class Employee
{
public String empName;
public int empSalary;
public String address;
//default constructor of the class
public Employee()
{
//this will call the constructor with String param
this("Chaitanya");
}
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
public Employee(String name)
{
//call the constructor with (String, int) param
this(name, 120035);
}
public Employee(String name, int sal)
{
//call the constructor with (String, int, String) param
this(name, sal, "Gurgaon");
}
public Employee(String name, int sal, String addr)
{
[Link]=name;
[Link]=sal;
[Link]=addr;
}
void disp() {
[Link]("Employee Name: "+empName);
[Link]("Employee Salary: "+empSalary);
[Link]("Employee Address: "+address);
}
public static void main(String[] args)
{
Employee obj = new Employee();
[Link]();
}
}
Output:
Employee Name: Chaitanya
Employee Salary: 120035
Employee Address: Gurgaon
Types of Inheritance:-
Java में तीन Inherita nce होते है |
1. Single Inheritance
2. MultiLevel Inheritance
3. Hierarchical Inheritance
1. Single Inheritance
Source Code :
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
//[Link]
class A{
void disp(){
[Link]("Parent Class");
class B extends A{
void show(){
[Link]("Child Class");
public static void main(String args[]){
B obj = new B();
[Link]();
[Link]();
Output :
Parent Class
Child Class
2. Multilevel Inheritance
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
Source Code :
//[Link]
class A{
void disp(){
[Link]("Class A");
class B extends A{
void show(){
[Link]("Class B");
class C extends B{
void getdata(){
[Link]("Class C");
public static void main(String args[]){
C obj = new C();
[Link]();
[Link]();
[Link]();
Output :
Class A
Class B
Class C
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
2. Hierarchical Inheritance
Source Code :
//[Link]
class A{
void disp(){
[Link]("Class A");
class B extends A{
void show(){
[Link]("Class B");
class C extends A{
void getdata(){
[Link]("Class C");
public static void main(String args[]){
C obj = new C();
[Link]();
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
[Link]();
B b = new B();
[Link]();
Output :
Class A
Class C
Class B
Multiple Inheritance
Multiple Inherita nce के सलए Java में एक से ज्यादा Base classes होते है और सभी base
classes को एक ह derived class inherit करता है |
Java में Multiple Inheritance; support नह ीं करता है ,ले ककन interface के माध्यम से multiple
Inheritance Java में support करता है |
Multiple Inheritance; Java में support क्यों नह ीं करता ?
Java में िब Multiple Inherita nce आता है , तब Ambiguity problem आ िाता है | Java में
extends के साथ कहा पर भी Multiple Inheritance का उल्ले ख नह ीं ककया गया है | ले ककन
interface में इसे इस्ते माल ककया िा सकता है |
Java में एक से ज्यादा parent class नह ीं हो सकते | Program में तीन class है | C class; A और
B इन दोनों class को inherit करता है |
Source Code :
Object Oriented Programming using ‘JAVA’
COVID -19 #Stay Home Stay Safe
//[Link]
class A{
void disp(){
[Link]("Class A");
class B{
void show(){
[Link]("Class B");
class C extends A, B{
void getdata(){
[Link]("Class C");
public static void main(String args[]){
C obj = new C();
[Link]();
[Link]();
[Link]();
Output :
Error.
Object Oriented Programming using ‘JAVA’