# Definition of inheritance #
It is a process in which a class acquire the properties (bole toh data) and behavior (bole toh method) of another class. The class who acquire is known as sub-class and other one is known as a super-class.
Example: Son or Daughter is a sub-class, while Mother or Father is a super-class.
# Need of inheritance #
1. Using inheritance a sub-class can re-use the variables and methods of super-class.
2. Using inheritance we can implement run-time-polymorphism in java, which is also known as dynamic binding.
3. Using inheritance we can specialize the things like Mango is a specialization of Fruit and Car is a specialization of vehicle.
# Types of inheritance #
1. single level inheritance: in this a super-class is inherited by a single sub-class.
example: a father having a child.
2. Multi level inheritance: in this a super-class is inherited by a sub-class, which is further inherited by another sub-class.
example: grand-father inherited by father, father is further inherited by son.
3. Multiple inheritance: in this two super-classes inherited by a single sub-class.
example: son inherits his mother and father.
note: In Java 'Multiple inheritance' is not supported, because it may lead into data and code ambiguities.
4. Hybrid inheritance: it is a combination of multi-level and multiple inheritance, it is also not supported in java.
5. Tree like inheritance: in this a super-class is inherited by multiple sub-classes, those are further inherited by their sub-classes.
example: our family tree
# Syntax to inherit a class #
class Foo
{
}
class Doo extends Foo
{
}
note#1: now Doo will become a sub-class for Foo
note#2: extends is a keyword in java
# Things those never inherits from super-class to sub-class #
1. private variables and private method of super-class
2. constructor of super-class
3. blocks of super-class (like static block of init-block)
# Some very interesting facts about inheritance #
1. there is IS-A relationship between the sub-class and super-class.
For example:
Cat IS-A Animal
Alto IS-A Car
Employee IS-A Person
Mango IS-A Fruit
2. a sub-class may use any variable and method of super-class except private, but vice-versa not true
3. a sub-class belongs to its super-class as well as all of its super-classes, but vice-versa not true
For example:
Dog belongs to the Animal family, but Animal does not belongs to Dog, because all Animals are not Dog, some are Cat or Tiger also.
# what happens when we create object of sub-class #
when we create object of a sub-class the JVM (Java Virtual Machine) will doo the following
1. JVM loads the bytecode of super-class then the bytecode of sub-class will be loaded
2. JVM executes the constructor of super-class then the constructor of sub-class will be executed
For example:
Animal is a superclass and Dog is a subclass of Animal.
When object of Dog is created the constructor of Animal class will be executed first, then the constructor of Dog will be executed.
# How to access the non static members of superclass from the subclass #
A subclass can access non static variables/methods of superclass using super keyword
example:
class Father
{
String name = "Kamal Kishore"; // non static variable
void eat() // non static method
{
}
}
class Son extends Father
{
void display() // non static method of subclass
{
// access variable of superclass
System.out.println("name is "+super.name);
// access method of superclass
super.eat();
}
}
note: the super keyword can be used in non static methods only, and super cant access the private members of superclass.
# # # # # # # # # # # # # # # # # # #
# Data shadowing
When we declare same variable name in subclass which is also present in superclass.
Rule of data shadowing is name of variables must be same, the datatype does not matter
example:
class Father
{
String name = "Ram sukh";
}
class Son extends Father
{
String name = "Sukh ram";
}
# # # # # # # # # # # # # # # # # # #
# Method hiding
When we define same static method in subclass which is also present in superclass
Rule: method name plus parameter list plus return type must be same
example:
class Father
{
static void qualification()
{
}
}
class Son extends Father
{
static void qualification()
{
}
}
# # # # # # # # # # # # # # # # # # #
# Method overriding
When we define same non static method in subclass which is also present in superclass
Rule: method name plus parameter list plus return type must be same
example:
class Father
{
void qualification()
{
assume father is just BA
}
}
class Son extends Father
{
void qualification()
{
son is PHD
}
}
When we assign the object of subclass inside the reference variable of superclass.
example:
If Animal is a superclass and Dog and Lion are subclasses.
Animal a = new Dog();
or
Animal b = new Lion();
# The Runtime polymorphism
It is also known as dynamic binding or late binding.
need: Using Runtime polymorphism a superclass can access methods of subclass.
terms and conditions: the method must be non static and it must be present in superclass also.
It is a process in which a class acquire the properties (bole toh data) and behavior (bole toh method) of another class. The class who acquire is known as sub-class and other one is known as a super-class.
Example: Son or Daughter is a sub-class, while Mother or Father is a super-class.
# Need of inheritance #
1. Using inheritance a sub-class can re-use the variables and methods of super-class.
2. Using inheritance we can implement run-time-polymorphism in java, which is also known as dynamic binding.
3. Using inheritance we can specialize the things like Mango is a specialization of Fruit and Car is a specialization of vehicle.
# Types of inheritance #
1. single level inheritance: in this a super-class is inherited by a single sub-class.
example: a father having a child.
2. Multi level inheritance: in this a super-class is inherited by a sub-class, which is further inherited by another sub-class.
example: grand-father inherited by father, father is further inherited by son.
3. Multiple inheritance: in this two super-classes inherited by a single sub-class.
example: son inherits his mother and father.
note: In Java 'Multiple inheritance' is not supported, because it may lead into data and code ambiguities.
4. Hybrid inheritance: it is a combination of multi-level and multiple inheritance, it is also not supported in java.
5. Tree like inheritance: in this a super-class is inherited by multiple sub-classes, those are further inherited by their sub-classes.
example: our family tree
# Syntax to inherit a class #
class Foo
{
}
class Doo extends Foo
{
}
note#1: now Doo will become a sub-class for Foo
note#2: extends is a keyword in java
# Things those never inherits from super-class to sub-class #
1. private variables and private method of super-class
2. constructor of super-class
3. blocks of super-class (like static block of init-block)
# Some very interesting facts about inheritance #
1. there is IS-A relationship between the sub-class and super-class.
For example:
Cat IS-A Animal
Alto IS-A Car
Employee IS-A Person
Mango IS-A Fruit
2. a sub-class may use any variable and method of super-class except private, but vice-versa not true
3. a sub-class belongs to its super-class as well as all of its super-classes, but vice-versa not true
For example:
Dog belongs to the Animal family, but Animal does not belongs to Dog, because all Animals are not Dog, some are Cat or Tiger also.
# what happens when we create object of sub-class #
when we create object of a sub-class the JVM (Java Virtual Machine) will doo the following
1. JVM loads the bytecode of super-class then the bytecode of sub-class will be loaded
2. JVM executes the constructor of super-class then the constructor of sub-class will be executed
For example:
Animal is a superclass and Dog is a subclass of Animal.
When object of Dog is created the constructor of Animal class will be executed first, then the constructor of Dog will be executed.
# How to access the non static members of superclass from the subclass #
A subclass can access non static variables/methods of superclass using super keyword
example:
class Father
{
String name = "Kamal Kishore"; // non static variable
void eat() // non static method
{
}
}
class Son extends Father
{
void display() // non static method of subclass
{
// access variable of superclass
System.out.println("name is "+super.name);
// access method of superclass
super.eat();
}
}
note: the super keyword can be used in non static methods only, and super cant access the private members of superclass.
# # # # # # # # # # # # # # # # # # #
# Data shadowing
When we declare same variable name in subclass which is also present in superclass.
Rule of data shadowing is name of variables must be same, the datatype does not matter
example:
class Father
{
String name = "Ram sukh";
}
class Son extends Father
{
String name = "Sukh ram";
}
# # # # # # # # # # # # # # # # # # #
When we define same static method in subclass which is also present in superclass
Rule: method name plus parameter list plus return type must be same
example:
class Father
{
static void qualification()
{
}
}
class Son extends Father
{
static void qualification()
{
}
}
# # # # # # # # # # # # # # # # # # #
When we define same non static method in subclass which is also present in superclass
Rule: method name plus parameter list plus return type must be same
example:
class Father
{
void qualification()
{
assume father is just BA
}
}
class Son extends Father
{
void qualification()
{
son is PHD
}
}
benefit of method overriding: it is used to change the behavior of superclass method.
# # # # # # # # # # # # # # # # # # #
# Polymorphic assignment
When we assign the object of subclass inside the reference variable of superclass.
example:
If Animal is a superclass and Dog and Lion are subclasses.
Animal a = new Dog();
or
Animal b = new Lion();
# The Runtime polymorphism
It is also known as dynamic binding or late binding.
need: Using Runtime polymorphism a superclass can access methods of subclass.
terms and conditions: the method must be non static and it must be present in superclass also.
No comments:
Post a Comment