DAY 03- Object Oriented Programming-continue
========================================================================
TOPICS
TOPICS
- Inheritance
- Method Overriding
- Super Keyword
- Types of Inheritance
- Access Modifier In Inheritance
INHERITANCE
Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
In the terminology of Java, a class that is inherited is called a superclass. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass. It inherits all of the members defined by the superclass and adds its own, unique elements.
To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword.
Lets understand with the help of example.
As per the above example we can say that child class i.e. Test.java inherits all the properties and functionality of parent class if they are not private.
So if we want to call the same functionality in different classes we can use inheritance so that we need not to re-define the function again and again.
Note:- we can call instance as well as static members and function of parent class.
Now suppose a condition when we have same function and same type of data members in both the class.
Instance function :- If we have same instance function in both the classes the child class function override the parent class function.
Instance data members:- If we have same instance data members in both the classes then preference goes to local i.e. child class data members.
Static function :- If we have same static function in both the classes then preference goes to local i.e. child class data members however we can access the static function of parent class using class name.
Static data members:- If we have same static data members in both the classes then preference goes to local i.e. child class data members however we can access the static data member of parent class using class name.
Example for static data member as well as function.
Note:-
- Static function can't be override and this concept is know as function hiding.
- Data Members can't be override.
- Constructor can't be inherited they never become a part of child.
SUPER KEYWORD
Example:-
CONSTRUCTORS IN INHERITANCE
If we have constructor present in the base class then child class constructor always run parent class default constructor first.
Now let take a look for parameterized constructor.
if a parent class is having any constructor except default then child class must have to call that constructor explicitly and for this we have to use super keyword in our class.
We know that default constructor will only be placed by compiler if class doesn't have any constructor neither default nor parametrize.
Now suppose we have a parameterize constructor in child class then, does it mean it run the default constructor of parent class ? ----> Yes.
USE OF SUPER KEYWORD
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
TYPE OF INHERITANCE
- Single Level Inheritance :- TIll now what we have covered so far is known as single level inheritance which means one parent and one child.
- Multiple Inheritance :- Not supported by Java.
- Multilevel Inheritance :- In multilevel inheritance, As you can see in below flow diagram C3 is subclass or child class of C2 and C2 is a child class of C1.
- Hierarchical Inheritance. In hierarchical inheritance a parent class can have multiple child class.
- Hybid Inheritance : Not supported by Java.
Note:- Java does not support multiple and hybrid inheritance due to diamond problem.
There are 4 types of java access modifiers:
- private
- default
- protected
- public
Note :-
Child class function must have stronger specifier or same specifier as in base class function because child class cannot reduce the visibility of the inherited method from parent.
Example
It means we should consider the visiblity of method in both the classes in inheritance. below is the sequence from stronger to weaker modifier.
1. Public
2. Protected
3. Default
4. Private. :- private method can't be accessed outside of the class. it means that we can't access private members of class in another class using super keyword.
========================================================================
Links for the files (.Java File)
Links for the files (.Java File)
https://drive.google.com/open?id=0B5M4BcbfVN07VlJ4SDRFMmYzQnc
Links for recorded Video
https://www.youtube.com/watch?v=5LOW5OAIHT4&t=481s
========================================================================
Links for recorded Video
https://www.youtube.com/watch?v=5LOW5OAIHT4&t=481s
========================================================================
Post a Comment