• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Can we access sub class methods without using subclass reference?

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

Can we access sub class methods without using subclass reference?
Note: We can access only super class

regards
Sridhar
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method is declared in the super class and is part of the inheritance tree, then the super class reference type will automatically cast to the implementation type at runtime. If however you add functionality that is not in the super class, the super class reference does not know about this functionality, hence you have to explicitly cast down to the implementation type before you can use it.
 
Sri Dharan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sebastian ,
Thanks for valuable reply...
Super class:A
public class A{

public A(){

}

public String getStr(){
return "abc";
}
}
-------------------------
Sub Class: B

public class B extends A{

public B(){

}

public String getStrOne(){
return "1111111111";
}

public String getStrTwo(){
return "2222222222";
}

}
---------------------------
Could you please explain with this example?

Regards
Sridhar
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ...

please put your code between the code tags. It makes things much easier.

class A has one method: getStr()

If you declare A a = new A(), you have a reference variable a of type A which points to the implementation of A.
The reference is what defines what behavior is available.

If you declare A b = new B(), this is perfectly legal since B is of type A.
Now, since B is assigned to a reference variable of A, and A has no clue that B has getStrOne and getStrTwo, the only method you can call is getStr().
Remember, if you design a class you have no idea what other classes might extend yours in the future.

However, if you call getStr() on a (which is referenced to new B()), and B overrides getStr(), the JVM will figure that out and automatically execute the version of getStr int B.

If you declare B b = new B(), you inherit the getStr method from A, which means you are free to call b.getStr(). In case you did not override anything, the getStr method of A is invoked.

Okay, that was quite a lot of input. Hope that helps.
 
You didn't tell me he was so big. Unlike this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic