• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java abstract class

 
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello there,
I want to know how can i access non-abstract methods of an abstract class by a reference of child class??what if non-abstract method is overridden in child class??
Thanks.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:by a reference of child class??


one correction, by instance of child class.

shashank dwivedi wrote:
what if non-abstract method is overridden in child class??


simple test will answer you
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch shashank dwivedi
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way to access abstract class methods is by child-objects. Rest, as Seetha said you can test.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As said, you need to try out such queries by writing to a program. If at all you arent able to figure out even after writing a program, you can post the code here and get it clarified. It will help in learning the language in a much better way
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:As said, you need to try out such queries by writing to a program. If at all you arent able to figure out even after writing a program, you can post the code here and get it clarified. It will help in learning the language in a much better way

I tried it sir but can't do it.I have posted my program.Thanks for your help!
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

Mohamed Sanaulla wrote:As said, you need to try out such queries by writing to a program. If at all you arent able to figure out even after writing a program, you can post the code here and get it clarified. It will help in learning the language in a much better way

I tried it sir but can't do it.I have posted my program.Thanks for your help!



As said here, please UseOneThreadPerQuestion.

We would want you to post your code in this thread so that there is some continuity in the discussions.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

Mohamed Sanaulla wrote:As said, you need to try out such queries by writing to a program. If at all you arent able to figure out even after writing a program, you can post the code here and get it clarified. It will help in learning the language in a much better way

I tried it sir but can't do it.I have posted my program.Thanks for your help!


abstract class poly
{
public int l,b;
poly(int x,int y)
{
l=x;
b=y;
}
void disp()
{
System.out.print("inside poly");
}
abstract void area();
}
class rect extends poly
{
rect(int x,int y)
{
super(x,y);
}
void area()
{
System.out.println(l*b);
}

@Override
void disp()
{
System.out.println("I am rectangle");
}
}
public class refconcept
{
public static void main(String args[])
{
poly ref;
rect r=new rect(10,10);
poly refer=new rect(10,5);
if(r instanceof poly)
{
ref=r;
ref.area();
ref.disp();
refer.area();
refer.disp();

}
}
}

output:
100
I am rectangle
50
I am rectangle
BUILD SUCCESSFUL (total time: 0 seconds)

By any means i am unable to call method disp() in super class even using "super" keyword.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once the method is overridden, the binding works at runtime and the version of the method invoked depends on the type of the instance. In your case even though the type of reference is Poly, but the instance is of type Rect, hence disp() implementation in Rect is invoked always.

Please note to UseCodeTags so that code is more readable.

Also its recommended to use CamelCase naming convention for your Class Names. You can find details about formatting your code in the Javaranch Style Guide.

 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also you dont need to add an instanceof check.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An abstract method is meant to be overridden, why not make disp also abstract? Infact that is what you should be doing to support the fact that poly is an abstract class.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akhilesh Trivedi wrote:An abstract method is meant to be overridden, why not make disp also abstract? Infact that is what you should be doing to support the fact that poly is an abstract class.



area() method is abstract in Poly class.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote: area() method is abstract in Poly class.


Why not also disp? because you are wondering to be able to access a concrete method of an abstract class and you are also at the same time overriding it. You can't think both up and down the hierarchy at the same time, especially when you are done defining something at very top level - 'this is my abstract thing'.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akhilesh Trivedi wrote:An abstract method is meant to be overridden, why not make disp also abstract? Infact that is what you should be doing to support the fact that poly is an abstract class.


ya I can but why then i can't use Interface? Just checking why can i call super class methods...
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

Akhilesh Trivedi wrote:An abstract method is meant to be overridden, why not make disp also abstract? Infact that is what you should be doing to support the fact that poly is an abstract class.


ya I can but why then i can't use Interface? Just checking why can i call super class methods...


There is "super" keyword but i wonder why it is not working here?
Is there a way using Typecasting??
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

shashank dwivedi wrote:

Akhilesh Trivedi wrote:An abstract method is meant to be overridden, why not make disp also abstract? Infact that is what you should be doing to support the fact that poly is an abstract class.


ya I can but why then i can't use Interface? Just checking why can i call super class methods...


There is "super" keyword but i wonder why it is not working here?
Is there a way using Typecasting??


super() in your example is used to initialize the variables declared in the Poly class. And nothing related to invoking any of the methods in your Poly or Rect class.

You can try adding: disp() in the poly(int y, int y) constructor.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:

shashank dwivedi wrote:

shashank dwivedi wrote:

Akhilesh Trivedi wrote:An abstract method is meant to be overridden, why not make disp also abstract? Infact that is what you should be doing to support the fact that poly is an abstract class.


ya I can but why then i can't use Interface? Just checking why can i call super class methods...


There is "super" keyword but i wonder why it is not working here?
Is there a way using Typecasting??


super() in your example is used to initialize the variables declared in the Poly class. And nothing related to invoking any of the methods in your Poly or Rect class.

You can try adding: disp() in the poly(int y, int y) constructor.


Oh sir you get me wrong.I am not talking about super i used in constructor rather i meant can i some how use "super" keyword to call non-abstract method in super class( which is abstract)?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:
Oh sir you get me wrong.I am not talking about super i used in constructor rather i meant can i some how use "super" keyword to call non-abstract method in super class( which is abstract)?


Sorry for not getting your query.
Secondly, did you try adding a super.methodName() in the disp() method?
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:

shashank dwivedi wrote:
Oh sir you get me wrong.I am not talking about super i used in constructor rather i meant can i some how use "super" keyword to call non-abstract method in super class( which is abstract)?


Sorry for not getting your query.
Secondly, did you try adding a super.methodName() in the disp() method?


Yes it is working fine if i use it in child class but in know way i can access it from main class with any reference whether it is of parent class or child class.
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

Mohamed Sanaulla wrote:

shashank dwivedi wrote:
Oh sir you get me wrong.I am not talking about super i used in constructor rather i meant can i some how use "super" keyword to call non-abstract method in super class( which is abstract)?


Sorry for not getting your query.
Secondly, did you try adding a super.methodName() in the disp() method?


Yes it is working fine if i use it in child class but in know way i can access it from main class with any reference whether it is of parent class or child class.



what if i do something like
parent ref;
ref.MethodName();

but it simply displays overridden method in child class.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:

shashank dwivedi wrote:

Mohamed Sanaulla wrote:

shashank dwivedi wrote:
Oh sir you get me wrong.I am not talking about super i used in constructor rather i meant can i some how use "super" keyword to call non-abstract method in super class( which is abstract)?


Sorry for not getting your query.
Secondly, did you try adding a super.methodName() in the disp() method?


Yes it is working fine if i use it in child class but in know way i can access it from main class with any reference whether it is of parent class or child class.



what if i do something like
parent ref;
ref.MethodName();

but it simply displays overridden method in child class.



If the subclass overrides a method, and doesn't provide an alternate way to get to it, then "no" -- in that example, there is no way to access a method that have been overridden. And this is true regardless of whether the superclass is abstract or not.

Henry
 
shashank dwivedi
Ranch Hand
Posts: 72
MySQL Database AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

shashank dwivedi wrote:

shashank dwivedi wrote:

Mohamed Sanaulla wrote:

shashank dwivedi wrote:
Oh sir you get me wrong.I am not talking about super i used in constructor rather i meant can i some how use "super" keyword to call non-abstract method in super class( which is abstract)?


Sorry for not getting your query.
Secondly, did you try adding a super.methodName() in the disp() method?


Yes it is working fine if i use it in child class but in know way i can access it from main class with any reference whether it is of parent class or child class.



what if i do something like
parent ref;
ref.MethodName();

but it simply displays overridden method in child class.



If the subclass overrides a method, and doesn't provide an alternate way to get to it, then "no" -- in that example, there is no way to access a method that have been overridden. And this is true regardless of whether the superclass is abstract or not.

Henry


You cleared my all confusions i was trying to fix from last day.Thank you
But I managed to do that with a NULLPointerException which i handled.
Anyway thank you once again.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shashank dwivedi wrote:
You cleared my all confusions i was trying to fix from last day.Thank you
But I managed to do that with a NULLPointerException which i handled.
Anyway thank you once again.


I am wondering if you read my very first reply. May be I wasn't clear in what I said.
reply
    Bookmark Topic Watch Topic
  • New Topic