Java / Android Developer, Trainer, Writer, Speaker

Wednesday, January 28, 2015

On 1:31 AM by Unknown in ,    4 comments

Like the article?

Today we are going to learn Inheritance in JAVA. I've created four java class file. Those are:

1. fatherswealth.java ( Superclass )
2. lawyer.java ( Main Class )
3. myself.java ( Subclass )
4. mybrother.java  ( Subclass )





Here are the codes:

1. fatherwealth.java

/*
 Inheritance means inheriting stuffs ( variables, methods etc ) from other class.
 
  Why inheritance is important?
  Well, if we need same stuffs for many classes. We need to write code in every class
  rather doing that we will write same thing in one class and access that from other
  class through inheritances.

 */
package myinheritence;
//my java super class
public class fatherswealth {
public static void fatherswealth() {
// TODO Auto-generated method stub
System.out.println("My car, flat, lands all are for my two sons!");
}
}


2.  myself.java 


package myinheritence;
//my Java Sub class
public class myself extends fatherswealth{
/* in java writing Extends after class name means, you are inheriting.
* Here we are going to inherit everything from the fathers Wealth class.
*/
public void fromFather(String[] args) {
//System.out.println("my father says,  ");
}
}




3. mybrother.java 


package myinheritence;
/* This is my my java subclass.

 Note: we will inherit a class by using the keyword EXTENDS


*/
public class mybrother  extends fatherswealth{
// in this mybrother class we are also inheriting the class fatherswealth
public void fromFather(String[] args) {
System.out.println("printing from mybrother dot java class, My father says,  ");
// calling by the power of inheritance
fatherswealth fb= new fatherswealth();
fb.fatherswealth();
// we can use super keyword to call also from super class
super.fatherswealth();
System.out.println("Ends printing frtom my brother dot java class");
}
}




4. lawyer.java


package myinheritence;
public class lawyer {
public static void main( String[] args ) {
/*
Now i will create the object from the myself class to access the class.
 as the class is inherited, i can access the fatherwealth class through mybrother class
*/
myself meObj = new myself();
mybrother myBro = new mybrother();
/* object and method name separated by dot for printing the outcomes which are
inherited from the fatherwealth class
*/
// calling inheritance for my brother.
myBro.fromFather(args);
myBro.fatherswealth();
// calling inheritance from me
meObj.fromFather(args);
meObj.fatherswealth();
/*
if you use the java Static keyword in method, you can access it without creating object.
i used static keyword in fatherswealth class, so i can call it without object like below example.
*/
fatherswealth.fatherswealth();
}
}
/* Note 1: only public methods can be inherited.
Note 2: If you want to keep one method as same as inherited class, you can write that in your subclass by overriding.
Note 3: If you inherit a superclass and that superclass inherits another class. then you will access your immediate superclass
and also the superclass that your superclass accessed.
Note 4: A static and final method can not be overridden.
Note 5: Method overloading deals with the notion of
having two or more methods(functions) in the same class with the same name but different arguments.


*/ 

4 comments:

  1. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    microsoft azure training in bangalore
    rpa interview questions and answers
    automation anywhere interview questions and answers
    blueprism interview questions and answers
    uipath interview questions and answers
    rpa training in bangalore

    ReplyDelete
  2. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    Best Devops Training in pune
    Devops Training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  3. I’m delighted to have found your webpage and truly appreciate the valuable insights you’ve shared. The information about the SAP Production Planning Course, SAP ABAP Training Online, SAP FICO Online Training, SAP MM Training, SAP Extended Warehouse Management Training, and SAP QM Training is incredibly helpful for learners aiming to build strong SAP skills. Thank you for presenting each topic so clearly and making complex business processes easy to understand!

    Best SAP FICO Training in chennai

    Best SAP MM Training in Chennai

    Best SAP ABAP Training in chennai

    Best SAP PP Training in Chennai

    Best SAP EWM Training in chennai

    Best SAP QM Training in chennai

    ReplyDelete