Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract?  RSS feed

 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i've a few questions on abstract and hope can get some answers here.

public abstract double earnings();

1. is actract being used when we know that the method is going to be overwrite in subclass for sure?

2. we can overwite superclass's method even though without the word abstract on the method, right? if that so, in what kind of circumstances when we must use abstract?

3. what about abstract are on a class?

public abstract class Employee{}
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is a class that only defines methods - it has no actual implementation code. We cannot instantiate an interface.

An abstract class is any class that contains abstract methods (and possibly some non-abstract methods too). We cannot instantiate an abstract class. We would use abstract when we want to provide some implementation (methods) ourselves, but leave some (the abstract bits) up to the subclass. Contrast this to an interface where we leave all implementation to the implementing class.

I hope this helps to answer your questions.
[ May 14, 2005: Message edited by: Stuart Gray ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying my very best to learn java, please forgive me if i'm asking some really stupid questions
i myself, as a java student too, think there's no stupid Qs when trying to understand something (only embarrassed ones )
1. is actract being used when we know that the method is going to be overwrite in subclass for sure?
no, i dont think so; one uses abstract when we dont want to define the method boby; any method without a body is abstract; any class with abstract methods must be declared abstract too

in what kind of circumstances when we must use abstract?
when you dont want to define the behaviour to that method, but notice that must, in my opinion, isnt apropriated; you'r free to implement or not

anyway, in your case i'd wait for a more solid opinion in all this

EDIT this was posted before reading the answer one member posted
[ May 14, 2005: Message edited by: miguel lisboa ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overlapping responses here ... I went to lunch with the message editor open and you guys beat me back!

---

The keyword abstract on a method requires somebody down the road to override the method before they can make a concrete class and instantiate an object.

The keyword abstract on a class means nobody can instantiate this class, so they'll have to extend it to make a concrete class.

We could make an abstract class with no abstract methods, but if a class has any abstract methods the class must be abstract, too. The compiler also forces a class to be abstract if it implements an interface but doesn't implement all the methods defined in the interface.

Abstract classes are often partial implementations. I might fill in most of the methods in an abstract class with basic behavior, but require you to override a couple to provide custom behavior.

Did that answer the right question?
[ May 14, 2005: Message edited by: Stan James ]
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, appreciate for the fast replies from all of u.

i think i understand about the explantion on abstract. thanks.

but i still doubtful about this:
The compiler also forces a class to be abstract if it implements an interface but doesn't implement all the methods defined in the interface


can you explain in more details about this sentence?
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can try one trick i often use myself:

in this case create a class and then make it implement any known interface; then, dont implement any interface's methods;

try to compile and read the compiler message

i guess you'll get the answer to your Q

after that you can declare your class abstract and recompile and see the effect
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract means that not all the methods are implemented - you know what they're called, what input params they take, and what they return, but there's no CODE that does anything.

when you implement an interface, you are saying "I promise that i will write code for all the methods defined in the interface."

if you say you will implement something, and then DON'T actually write the code, you're violating the terms of the contract. you can get around this by declaring your class that implements the interface as 'abstract'. you're now saying "i promise to write code for all the methods (the 'implements' part), but i'm not going to actually write code for all the methods in this class, which will be ok since nobody can actually instantiate me (the 'abstract' part). Only when someone comes along and creates a class that extends from your abstract class (and then THEY must write code for that method, or make THEIR class abstract, and so on).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!