This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Inheritance: is overriding interchangeable with implementing?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java inheritance, can the expression "overriding an abstract method" be used interchangeably with the expression "implementing an abstract method"?

I came across the following question in the book "OCA Java SE 8 Programmer I Study Guide" in Chapter 5, page 296, question #15:

Q: Which of the following is true about a concrete subclass? (Choose all that apply)
A. A concrete subclass can be declared as abstract.
B. A concrete subclass must implement all inherited abstract methods.
C. A concrete subclass must implement all methods defined in an inherited interface.
D. A concrete subclass cannot be marked as final.
E. Abstract methods cannot be overridden by a concrete subclass.

My answer was B & E. But the book says, the correct answer is only B.
My question is specifically about the option E. My initial thought was abstract methods must be implemented before being overridden. So why is option be incorrect? The book says, abstract methods must be overridden by a concrete subclass.
 
Ranch Hand
Posts: 42
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract method must be overridden in subclass is true .......option E) says cannot be overridden that’s why it is false

You have to choose right answer
 
Sheriff
Posts: 23970
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rustam Ahmedov wrote:My question is specifically about the option E. My initial thought was abstract methods must be implemented before being overridden. So why is option be incorrect? The book says, abstract methods must be overridden by a concrete subclass.



How could they possibly be implemented without having the implementation override the abstract method?

I know, sometimes it's hard to deal with all of that abstract (ha ha) terminology. I often find it useful to write some actual code to illustrate the problem. So: here's a class with an abstract method:



See if you can write a class including a getAnswer() method which implements that abstract method without overriding it.
 
Rustam Ahmedov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




See if you can write a class including a getAnswer() method which implements that abstract method without overriding it.



Hi Paul! Thank you for getting back to me on this question.
Sure. In the following code, the way I have understood so far is that I am implementing the abstract method getAnswer(). I am not sure if I am overriding it.

 
Paul Clapham
Sheriff
Posts: 23970
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rustam Ahmedov wrote:Sure. In the following code, the way I have understood so far is that I am implementing the abstract method getAnswer(). I am not sure if I am overriding it.



Yes, that's a good example of implementing the method. As for whether you're overriding it: if you weren't overriding the method there would be no point in writing an unrelated method. If you're overriding a method it's often useful to include the @Override annotation to make sure you did it right. Like this:



In this code, if the getAnswer() method in SubTest doesn't override the getAnswer() method in Test, then the compiler will produce an error referring to the method. If you like, you could compile those two classes and confirm that SubTest's getAnswer() method does indeed override the method in Test.
 
Rustam Ahmedov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In this code, if the getAnswer() method in SubTest doesn't override the getAnswer() method in Test, then the compiler will produce an error referring to the method. If you like, you could compile those two classes and confirm that SubTest's getAnswer() method does indeed override the method in Test.



I compiled the two classes and the method in the SubTest class did override without compiler error. So, would it be fine if I conclude that we implement abstract methods by overriding them? If yes, is there any other ways of implementing abstract methods?  
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner .
So my concern is how can we override the abstract method . Because overriding is possible in already implemented method which we called overridden method.        Abstract class has method but not with the implementation . Only concrete class can implement abstract method . So how can we override the abstract method ?

In the code I made some changes showing overridden and overriding method according to me
I don’t know if I am wrong


public class SubTest extends Test{
     public String getAnswer(){.        \\ Overridden method
            return "the answer";
    }
}
Class sub extends SubTest {
Public String getAnswer(){.        \\  Overriding method
return “answer”;
}
}
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sorry i was wrong.........finally i got it

abstract class MyClass{
  public void disp(){
    System.out.println("Concrete method of parent class");
  }
  abstract public void disp2();
}

class Demo extends MyClass{
  /* Must Override this method while extending
   * MyClas
   */
  public void disp2()
  {
      System.out.println("overriding abstract method");
  }
  public static void main(String args[]){
      Demo obj = new Demo();
      obj.disp2();
  }
}
 
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I shall add line 19 and a new class, again overriding the disp2 method.
Let's look up abstract methods in the Java® Language Specification (=JLS).

That JLS Section wrote:Every subclass of A that is not abstract (§8.1.1.1) must provide an implementation for m, or a compile-time error occurs.

An abstract class can override an abstract method by providing another abstract method declaration.

Another version of your code to show an abstract class with all methods implemented. I have made a naughty change to your disp2 method, and shall let you work out what it does now
See what happens if you insert super.disp2(); between the current lines 25‑26.
 
Shweta Priyadarshi
Ranch Hand
Posts: 42
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes we can override it by extending another class .


if you insert super.disp2(); between the current lines 25‑26.

It will print super class version “ overriding abstract method”
 
Paul Clapham
Sheriff
Posts: 23970
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rustam Ahmedov wrote:So, would it be fine if I conclude that we implement abstract methods by overriding them?



Yes.

If yes, is there any other ways of implementing abstract methods?  



No.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!