Java / Android Developer, Trainer, Writer, Speaker

Wednesday, January 28, 2015

On 1:31 AM by Unknown in ,    3 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.


*/ 

3 comments: