Inheritance in Java

In programming we always try to write short code and avoid repetition of same code. Inheritance is useful in this work very much. Just like real life, we can create a family hierarchy in programming just like 4-wheeler -> car -> farari. Each child class ( called sub class) inherits all public and protected property (variables, methods, etc.). For example we have two classes one is Father and another is Child.

  1. package findalltogether;
  2. class Father
  3. {
  4. // father's public property
  5. public int father_int = 12;
  6. // father's protected (inheritable property)
  7. protected void who_i_am()
  8. {
  9. System.out.println("I am Father");
  10. }
  11. // father's inheritable property
  12. protected void father_property()
  13. {
  14. System.out.println("this is father's property");
  15. }
  16. }
  17. public class Child extends Father
  18. {
  19. // modifying father's property
  20. public void who_i_am()
  21. {
  22. System.out.println("I am Child");
  23. }
  24. // accessing father's property using super keyword
  25. public void access_father()
  26. {
  27. System.out.println("father variable is "+super.father_int);
  28. }
  29. // child is adding its own property
  30. public void child_property()
  31. {
  32. System.out.println("It is child's property");
  33. }
  34. public static void main(String args[])
  35. {
  36. Child object = new Child();
  37. // child object using its own method
  38. object.who_i_am();
  39. object.child_property();
  40. // child object using father's method
  41. object.father_property();
  42. // using father's variable
  43. System.out.println(object.father_int);
  44. object.access_father();
  45. }
  46. }

Output:

  1. I am Child
  2. It is child's property
  3. this is father's property
  4. 12
  5. father variable is 12

Now we have two classes. Look at the syntax of Child class. It is extending Father class and inheriting all its public and protected properties. Properties of inheritance is:

  • Child (sub) class inherits all public and protected instances of the parent (super) class.
  • Child class can use these instances directly without defining them using super keyword.
  • Child class object has modified instances (in child class) and remaining parent class instances. For example in main method, object has who_i_am() method from from Child class and father_property() method from Father class. In other words, we can say that every property (except private) of father is also property of child and child can modify this property. Child can also add its own property (Child class has its own method called child_property()).
  • Child can not low the visibility of the parent's instances. Visibility sequence is public > protected > private. So, parents public instances can only be public even in child class. Parent's protected instances can be public or protected (depends upon modification) in the child class. Child class can not access parent's private instances.
  • Inheritance is one way. We can not make any cycle like this a=> b=> c=> a or a=> b => c=> b.
  • Java allows single inheritance. One class can not inherit from more than one class. In other words, there can be only one father of one child.

Overriding:

When we overload the methods of the super class in sub class then it is called overriding.

  1. package findalltogether;
  2. class Father
  3. {
  4. protected void method()
  5. {
  6. System.out.println("this is parent function");
  7. }
  8. }
  9. public class Child extends Father
  10. {
  11. //method overriding by changing parameter
  12. // we can also change return type along with parameter(s)
  13. protected int method(int a)
  14. {
  15. return a;
  16. }
  17. public static void main(String args[])
  18. {
  19. Child object = new Child();
  20. System.out.println(object.method(47));
  21. }
  22. }

Multiple level Inheritance:

Java allow hierarchy. For example: Child is inheriting Father which is inheriting Grandfather. It means that Child also inheriting from Grandfather indirectly because every non-private property of Grandfather is property of Father and all non-private property of Father is property of Child.

  1. package findalltogether;
  2. class Grandfather
  3. {
  4. protected void method()
  5. {
  6. System.out.println("method in grandfather");
  7. }
  8. protected void method2()
  9. {
  10. System.out.println("method2 in grandfather");
  11. }
  12. }
  13. class Father extends Grandfather
  14. {
  15. protected void method2()
  16. {
  17. System.out.println("method2 in father");
  18. }
  19. }
  20. public class Child extends Father
  21. {
  22. public static void main(String args[])
  23. {
  24. Child object = new Child();
  25. // method call from grandfather
  26. object.method();
  27. // method call from father
  28. object.method2();
  29. }
  30. }

OUTPUT:

  1. method in grandfather
  2. method2 in father

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Java is object oriented and platform independent programming language which runs on JVM. Lets see advantages of java ...
Introduction to Java
In this tutorial, we will learn how to give/ take input/ output to console. What are classes, main() and comments? Wh...
First program in Java
Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, str...
Variables and data types in Java

Login or Sign up to leave comment.