JAVA

Views:
 
Category: Education
     
 

Presentation Description

super class

Comments

Presentation Transcript

A superclass variable can reference a subclass object:

A superclass variable can reference a subclass object Presentation by, Revathi.S 09MTBI20 JAVA

OBJECTIVES:

Superclass variable can reference a subclass object Java program for Reference demo Keyword-’ SUPER ’ Using super to call superclass constructors Initialization of box attributes to BoxWeight using Super Java prog ., for implementation of Box weight General forms of super OBJECTIVES

Introduction:

Reference variable of superclass assigned to subclass Boxweight -subclass of box-superclass Introduction

Slide 4:

Plainbox is reference of box & weightbox is reference of boxweight when a reference to a subclass object is assigned to a superclass reference variable , you will have access only to those parts of the object defined by the superclass.

Slide 5:

Java prog .. For Reference variable

Slide 6:

class RefDemo { public static void main(String args []) { BoxWeight weightbox = new BoxWeight (3, 5, 7, 8.37); Box plainbox = new Box ();// superclass_name reference-variable=new object inialization double vol ; vol = weightbox.volume (); System.out.println ("Volume of weightbox is " + vol ); System.out.println (" Weight of weightbox is " + weightbox.weight ); System.out.println ();

Slide 7:

// assign BoxWeight reference to Box reference plainbox = weightbox ; vol = plainbox.volume (); //volume() defined in Box System.out.println ("Volume of plainbox is " + vol ); /* The following statement is invalid because plainbox doesnot define a weight member */ // System.out.println ("Weight of Plainbox is " + plainbox.weight ); } }

Using SUPER:

Subclass need to refer to its immediate superclass-’ super ’ keyword Two general forms. calls superclass constructor used to access the member of the super class that has been hidden by member of a subclass Using SUPER

Using super to call superclass constructor:

Subclass can call a constructor method defined by its superclass by use of the following form-SUPER Super(parameter-list); Using super to call superclass constructor

A second use of super:

second form of super Acts like ‘ this’- keyword Except that it always refers to the superclass of the subclass in which it is used A second use of super

this:

Refer to the object that invoked it Used inside any method to refer to the current object Always a reference to an object on which the method was invoked t his

General form:

Super.member Member-method or an instance variable situations - member names of a subclass hide members by the same name in the superclass General form

Slide 13:

Java prog .. for Class hierarchy

Slide 14:

//using super to overcome name hiding class A{ int i; } //creating a subclass by extending class A class B extends A{ int i ; // this i hides the i in A B( int a,int b){ super.i =a; // i in A i=b; // i in B }

Slide 15:

void show(){ System.out.println ("i in superclass:",+ super.i ); System.out.println ("i in subclass:",+ super.i ); } } class Usesuper { public static void main(String args []){ B subOb =new B(1,2); subOb.show (); } }

output:

i in superclass:1 i in subclass:2 output

Slide 17:

Instance variable i hides in B hides the i in A,super allows access to I in A Super also used to call methods that are hidden by subclass

Conclusion:

We have discussed about… Superclass variable can reference a subclass object Using super to call superclass constructors General forms of super calls superclass constructor used to access the member of the super class that has been hidden by member of a subclass Conclusion

References:

The Complete Reference JAVA 2-Third Edition-Patrick Naughton and Herbert Schildt .(pg:198-205) References

authorStream Live Help