aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Benefits of Reference Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Benefits of Reference Type" Watch "Benefits of Reference Type" New topic
Author

Benefits of Reference Type

Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
Hello every one

class A {

}

class B extends A{

}

class Test {
public static void main(Strings args[]){

A a = new B();//line 1
B b = new B();//line 2
}

}

what are the advantages of using reference type A as in line 1 compared to B

in line 2 . Please explain
Hasitha Randika
Ranch Hand

Joined: Sep 04, 2007
Posts: 50
hi i think you have heard about polymorphysm.

A a = new B(); this is polymorphysm the advantage of this is non static methods when overriding goes to the object type.

class A{
int x = 10;
void m(){
System.out.println("Mike");
}
}
class B extends A{
int x = 20;
void m(){
System.out.println("You");
}
}
class C{
public static void main(String ar[]){
A a = new B();
a.m();
System.out.println(a.x);
}
}

now here the out put will be You
but in second out put will be 10 other than non static methods all the others goes to the Referance type.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
The advantage is that you can write generic code for the superclass in which you can use the specialized behaviour of the subclass


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Joshua Fix
Ranch Hand

Joined: Sep 18, 2007
Posts: 57
What are the benefits when using collections, as I posted here:
http://www.coderanch.com/t/266667/java-programmer-SCJP/certification/polymorphism ?


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Benefits of Reference Type
 
Similar Threads
Variables and Methods?
Cast expression
Overloading
Following code shows ClassCastException
need help in arrays