• 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

Abstract class doubt

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi can anyone explain why the concrete method can be overridden with abstract method in a abstract class...Any pointers to explanation of the behaviour or extensions of this rule would be helpful...
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question you should be asking is Why not? Why shouldn't you be allowed to override a non-abstract method with an abstract one? I don't see a problem here.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Megha,

Making a concrete method abstract, we have following issues

1- First the overriding class must be abstract.
2- You are killing the availability of concrete implementation
of that method by making it abstract.
3- Further concrete classes must override that method extending
that class.
4- We may feel that now each extending class must give
definition to the methods, where previously concrete
implementation of that method was available.But by directly
making that class abstract will break the existing code.
So as the nature of the language, making a class that
extends the previously existing class but with the
abstract way, forcing future extending classes to
override the methods.


Thanks,
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra for the explanation

and Henrique Ordine sorry I didnt ask the question you wanted me to ask...
next time I will do that.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when tried with the code

class ewr
{
public void er(String ls)
{
String ls_nn;
ls_nn = ls;
System.out.println("OK " + ls_nn);
}
}

abstract class aer extends ewr
{
public abstract void er(String ls);
}

public class ok implements aer
{
String rr;
public void er(String rr)
{
System.out.println("OOOO");//super.er(rr);
}

public static void main(String a[])
{
String rr = "Babai";
ok o = new ok();
o.er(rr);
}
}


this is the error faced.
interface expected where abstract is made
"abstract class aer extends ewr"

now retried with the following piece of code
class ewr
{
public void er(String ls)
{
String ls_nn;
ls_nn = ls;
System.out.println("OK " + ls_nn);
}
}

interface class aer extends ewr
{
public abstract void er(String ls);
}

public class ok implements aer
{
String rr;
public void er(String rr)
{
System.out.println("OOOO");//super.er(rr);
}

public static void main(String a[])
{
String rr = "Babai";
ok o = new ok();
o.er(rr);
}
}
got the following error at same line getting error <identifier> expected


please suggest why is this happening.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes and interfaces are different things.

You can only use implements with an interface name.
 
Henrique Ordine
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry megha, I didn't mean to be offensive.
What I meant to say was that I can't think of a reason why someone would override a non-abstract method with an abstract method, but I also can't see what the problem would be if someone did. I guess, one explanation for that rule would be: to make java more flexible. In case someone is ever inventive or crazy enough to come up with a solution which would require that feature of the java language.
 
reply
    Bookmark Topic Watch Topic
  • New Topic