Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Thursday, 10 October 2013

Sub Class and Super Class Relationship - Java Inheritance

In today's discussion we will come across the relationship in between a Sub Class and a Super Class in Java. In java classes can be derived from another classes, the class that is being derived is called a subclass and the class from which the subclass is being derived is called a superclass. Lets see the implementation of superclass and subclass and try to figure out what can be done and what cant.


Super Class

src\com\beingjavaguys\core\Fruit.java

package com.beingjavaguys.core;

public class Fruit {

 public String pub = "FruitPublic";
 String def = "FruitDefault";
 protected String pro = "FruitProtected";
 private String pri = "FruitPrivate";

 public void name() {
  System.out.println("I am a fruit !");
 }

 public void contains() {
  System.out.println("I contains a number of fruits in me !");
 }

 private void myPrivateMethod() {
  System.out
    .println("This is for me only, no one can change my definition !");
 }

 protected void myProtectedMethod() {
  System.out.println("This is for me and my subclasses only !");
 }

 void defaultScope() {
  System.out.println("I am in defult scope of 'Fruit' class !");
 }

 public static void iAmStatic() {
  System.out.println("I am static to Fruit");
 }

 public final void iAmFinal() {
  System.out.println("I am final to Fruit");
 }

 public static final void iAmStaticFinal() {
  System.out
    .println("My final part protects me from being ovverriden or hiding !");
 }

}



Sub Class

src\com\beingjavaguys\core\Apple.java

package com.beingjavaguys.core;

public class Apple extends Fruit {

 /*
  * a subclass can override all non-private, non-static and non-final methods of its superclass
  */
 @Override
 public void name() {
  System.out.println("I am an Apple - ovverride !");
 }

 @Override
 protected void myProtectedMethod() {
  System.out
    .println("This is for me and my subclasses only - ovverride !");
 }

 @Override
 void defaultScope() {
  System.out.println("I am in defult scope of 'Apple' class !");
 }

 /*
  * private methods of a superclass can not be overriden in subclass, this
  * method is identical to one in superclass, but this is not overriden, this
  * is purely a separate method from one in superclass
  */
 private void myPrivateMethod() {
  System.out
    .println("This is apples's own method and not a overriden one !");
 }

 public void iAmApple() {
  System.out.println("Being apple is my personal attribute !");
 }

 /*
  * static methods of a superclass can not be overriden, as @Override
  * annotation can not be used here. This practice is called hiding the
  * superclass method instead
  */
 public static void iAmStatic() {
  System.out.println("I am static to Apple");
 }

 /*
  * public final void iAmFinal(){ 
  *  System.out.println("I am static to Fruit");
  * } 
  * we can not ovverride a final method, this is the way to protect an
  * superclass method to be overrriden in subclass 
  */
 
 
}



Implementation class code

src\com\beingjavaguys\core\Implementation.java

package com.beingjavaguys.core;

public class Implementation {

 public static void main(String args[]) {

  /*
   * Apple apple = new Fruit();
   * 
   * this will through an exception, we can't refer a superclass object
   * with a subclass reference variable
   */

  /*
   * Apple apple = (Apple) new Fruit();
   * 
   * This will work fine at compile time but at run time it will through
   * "ClassCastException"
   */

  /*
   * With a superclass instance all methods will be called of superclass
   * only, no matters if a subclass overrides them
   */
  Fruit fruit = new Fruit();
  fruit.name();
  fruit.contains();
  fruit.myProtectedMethod();
  fruit.defaultScope();

  /*
   * fruit.myPrivateMethod();
   * 
   * private methods of a superclass are not even visible to it's subclass
   */

  /*
   * fruit.iAmApple();
   * 
   * This is perfectly illegal, we can not call subclass members with a
   * superclass object
   */

  /*
   * This is simple, a subclass can access all non-private members of a
   * superclass, if overridden then overridden methods are called, else
   * the superclass declaration are called
   */
  Apple apple = new Apple();
  apple.name();
  apple.contains();
  apple.iAmApple();

  /*
   * apple.myPrivateMethod();
   * 
   * this is illegal, private members of superclass are not visible to a
   * subclass
   */

  /*
   * a superclass reference with subclass object can call only those non
   * private methods that are present in superclass if methods are
   * overriden, than overriden method will be called
   */
  Fruit appFruit = new Apple();
  appFruit.name();
  appFruit.contains();

  /*
   * appFruit.iAmApple(); this will through an compile time error,
   * non-overriden methods of a sub class can not be called from
   * superclass reference
   */

  /*
   * appFruit.myPrivateMethod(); this will through an compile time error,
   * even the private methods of a superclass can not be called from a
   * superclass reference to subclass object
   */

  /*
   * if subclass does not have same static method as in superclass, then
   * the subclass will call the one in superclass.
   */
  Fruit.iAmStatic();
  Apple.iAmStatic();

  /*
   * a final method can not be overriden by a superclass, even we cant
   * hide it in subclass
   */
  fruit.iAmFinal();
  apple.iAmFinal();
  appFruit.iAmFinal();

  /*
   * a static final method is used when we want a static method in
   * superclass to be protected from being hided by subclass
   */
  Fruit.iAmStaticFinal();
  Apple.iAmStaticFinal();

 }

}



Explanation

Although the code above is being decorated with required explanations, hence there can be a few point to remember for further reference :

1) We can not refer a superclass instance with a subclass's reference variable.
2) With a superclass instance all methods will be called of superclass, no matters if subclass override them.
3) No 'private' member of a superclass can be visible there in its subclass.
4) Subclass methods can not be called from superclass's instance.
5) A subclass instance can call all non-private members of superclass, if overridden then overridden methods are called, else the superclass declarations are called.
6) A superclass reference with subclass object can call only those non private methods that are present in superclass if methods are overriden, than overriden method will be called.
7) Static methods can not be overriden in subclass, instead static method can be hide in subclass by defining same method in subclass.
8) Final methods can not be overriden or hide in subclass, that is the reason we specify static final to protect a static method being hide.
9) A subclass can override all non-private, non-static and non-final methods of its superclass

In this tutorial we came across the relationship in between a subclass and a superclass, in our next tutorials we will see more abour Java Programming and other open source technologies.








Thanks for reading !
Being Java Guys Team




0 comments:

Post a Comment

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft