Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract methods  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about abstract classes and methods. If a non-abstract class contains one abstract method does the subclass of that class HAVE to implement the abstract method? What if the subclass is abstract....would that make a difference?
Thanks-
Liz
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any class that contains abstract method MUST have abstract as part of its declaration. Any subclass that is not declared as abstract class and extend an abstract superclass must implement all the abstract methods in the superclass. On the other hand, if the subclass is not an abstract class, then it does not have to implement the abstract methods in the superclass, though any class that extends the subclass must implement both the abstract methods in the superclass and the abstract methods in the subclass.
Any one correct me if I am wrong.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liz
First off, you can't hae a non-abstract class with an abstract method. Any class that has an abstract method must be declared as abstract itself. On the other hand, you can declare a class as abstract even if it doesn't have any abstract methods.
Now, to answer your question, the subclass doesn't have to implement the abstract method but, if it doesn't, then it too has to be declared as an abstract class.
hope that helps
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


On the other hand, if the subclass is not an abstract class, then it does not have to implement the abstract methods in the superclass, though any class that extends the subclass must implement both the abstract methods in the superclass and the abstract methods in the subclass


Such a subclass must be declared abstract, if it does not implement the abstract methods of the superclass. Any class that extends this subclass must either: implement the abstract methods of this subclass, or if it does not, be declared abstract itself.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony,
Chung was right. He just made a typing error.
I can see that he was right in the beginning, so
what he typed next was obviously just typing error.
 
Chung Huang
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks...I did mean to say that subclass that don't implement the abstract superclass need to be abstracted...What a bobo :roll:
I just hope I don't make the same stupid error on exam time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!