Inheritance simply means child inherits behavior and properties from parent.
Real life example like our laptop battery is discharge the charge it and reuse it.
Like Gas cylinder if empty then we refill it again. To save cost same thing in coding.
Without relation reusability is not possible.
Benefits of reusability in code is 1 already developed program used in another program. To avoid ambiguity in code
Car is a vehicle and bike is also vehicle and when you got the relation is (is-a) then always prefer for inheritance. If relation is (has –a) then go for association.
Car has a (engine, MusicSystem)When between 2 classes relation is (has-a) then use association.
But when we remove the engine then car become useless then its example of composition.
Music system has a relation with car but its an aggregation without music system we can have car.
In Coding we create constructor for strong and methods for weak things like we have an example of human, human have hair, heart, eyes human can live without hair but not without heart.
Whenever we create any class in java then it’s impossible to create without inheritance because implicitly we are inheriting Object Class implicitly. Means every class extending Object class and this is Super class for every class.
It’s a same class access approach now we use another way to accessing that data member and method by inheriting the class.
Note :
non static data get memory when object is created.
So create 1 child class
Then question is ,How can we access non static data inside any static method. Now you can access similar in child class as well.
Note:This is how can we access non-static method and data member in a class or outside the class by inheritance child can access all data member and method parent but not private and Parent can’t anything of child data member and method.
When in above program Parent want to access it child data or method then we will get an error.
//System.out.println("Show Child x:"+p.y);
//p.hiChild();
//p.helloChild();
When Parent class don't access child class data then it will happily run
Sol:
Sol: It simply means when we declare a variable in parent class suppose name x=10 and with the same name we declare variable in child class X=20 then when we print x then you will see that output will be 20 because child data hides the parent data . Its similar to Data shadowing, but here is 1 difference that data shadowing happen in 1 class but data Hiding happened in 2 class parent and child.
Note: whenever data hiding is done always preference goes to child class data.
Sol: When in a class instance variable name and local variable name same then preference goes to local variable it’s called data shadowing. When this shadowing between 2 class then its call data hiding.
Example of Data Hiding and Data Shadowing
It’s a Keyword in java which can be used in so many ways.
Note : super keyword not be used in static context(static method or static block) like this keyword .
Example of super Keyword
In java there is rule that object of child class can be hold in reference variable of Parent class this concept is called Upcasting.
In java there is rule that object of Parent class can be hold in reference variable of Child class this concept is called Upcasting.
Note: without upcasting downcasting is not possible.
Sol: Suppose we have 2 class 1 Parent and 1 child classes and when a parent have any method and the method same name is also present in child class with same type and Signature then its called method Overriding.
Note: Whenever method overriding is done always preference goes to the child class Overridden method.
Sol: Because to reuse code .when child wants all what in base logic and base concept as its but enhancement in child that is called Method overriding. You can see in the program. Here in vehicle start Method is and in Car class show method is enhance so you can say Car override method of Vehicle and its concept is called method overriding.
Method overloading we can achieve in same class or in the case of Parent and child class.
Output
when you call method with an arguement as 120 c.start(120); then it will call methods which is define in the child class.
Output
You will get compile time error because you are changing only return type and you want method overriding.then its not possible.
Output
Note: Any class default value is null.
Sol: But after jdk 1.5 you can achieve method overriding by changing only return type but condition is return type should not primitive type.
So what we done here we create relation between A and B in Vehicle class Return type is A so we make class A parent and B class child And B is return type of Car class so according to this we create relation ship between them and A and B are covariant because it’s a return type of Vehicle and Car in method overriding case and we can return null. Or return an object.
You can see here method overriding is happen. By using covariant type and changing only return type.
Example of covariant type, method overriding and method chaining
It is the process of calling multiple constructors in a chain when object is created.
Note: Both the keyword must be written in the first line of constructor body.
Rule: when we use super in child class constructor then Jvm ,before executing child class constructor first execute the immediate Parent class default constructor not parameterized constructor.
Sol: Because in order to initialize parent class Data member.
If we have multiple constructor in child class and want to call the default constructor of the Parent class, then we have to use super in every constructor. if we don’t use then compiler add implicitly. Because we can create object using any constructor either parameterized or non parameterized so , that’s why we write super keyword at the top of every constructor in child class . when we compile the java file then Compiler add super keyword at the top of every constructor in child class at compile time ,to call default constructor of parent class.
If we create parameterized constructor in parent class ,but we did not define any default constructor. And child class constructor use super keyword in its constructor ,so jvm will executes parent class constructor before executing child class constructor ,but it can call only Default constructor of Parent class and it was not there in Parent class constructor ,so you will get compile time error. Because when you didn’t create any constructor in the Parent class, then compiler add a default constructor in the Parent class ,but you create a parameterized constructor so compiler will not add default constructor so you will get an error. In this example you can see.
If you want to call multiple constructor of a same class in a chain ,then you can use this keyword .If we use this keyword at any constructor ,then we can’t use super key word in that constructor.
If a member call getting their member definition at compile time is called Static binding.Compile time binding done by compiler on the behalf of type of Reference variable.
Dynamic binding :If a member call get their member definition at runtime called dynamic binding.