Inheritance:- extends is the keyword for inheritance
Creation of new class by using existing class.
Eg.
class A
{
}
class B extends A
{
}
A is known as parent class/ base class/ super class.
B is known as child class/ derived class/ sub class.
Child class inherits the property of parent class. It means we can we members of parent class in the child class without any object.
Two methods of access a class are:-
1.using objects.
2.using inheritance.
Base class cannot access members of derived class.
We can access the members of parent class as well as members of child class through the object of child class.
Child class can be used in place of parent but parent cannot be used at place of parent.
Note:- All the classes by default are inherited from the Object class (present in java.lang package).
*Every class pass is a test for the Object class.
Types of inheritance:-
1.Single Inheritance
2.Hierchical inheritance
3.Multiple level inheritance
4.Multiple inheritance( not possible in java )
5.Hybrid Inheritance
Class A
{
}
Class B extends A
{
}
Class C extends B
{
}
*B can be used in place of A.
*C can be used in place of A and B.
*B can’t be used in place of C but can be used in place of A.
Eg.
Object hello(Object)
{
}
Object is a datatype and Object class is a super class.
We can pass object of any class(even null).
Eg.
void hello()
{
Return null;
}
It will show error as we are returning null
Note:-When we pass object as a argument then we call it by using any reference parameter.
When object is used as return type then any reference variable can be return and null can also be returned.


No comments:
Post a Comment