Java / Android Developer, Trainer, Writer, Speaker

Wednesday, January 28, 2015

On 1:31 AM by M Rubel in ,    6 comments

Like the article?

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

1. ( Superclass )
2. ( Main Class )
3. ( Subclass )
4.  ( Subclass )

Here are the codes:


 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!");


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,  ");


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();
// we can use super keyword to call also from super class
System.out.println("Ends printing frtom my brother dot java class");


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.
// calling inheritance from me
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.
/* 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.



  1. Replies
    1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.