While writing abstract class do you define any variables?
My question is: Why you do that? it doesn't make sense to me becouse
you can't make instance of abstract class..
I understand why to make non-abstract method in abstract class, but
this story with variables...as I said doesn't make sense (most probably I'm
wrong)
With interface as (100% abstract class) defining variable is even worse...
am I missing something? ;-)
THX
man4*.* - 17 Nov 2006 14:42 GMT
I found answer for my question...
"variables defined in interface are always public static and final"...which
means constants....
what a silly question I've asked... :-)
> While writing abstract class do you define any variables?
> My question is: Why you do that? it doesn't make sense to me becouse
[quoted text clipped - 9 lines]
>
> THX
Jason Cavett - 17 Nov 2006 14:56 GMT
First off, do you understand the differences between an abstract class
and an interface.
An abstract class defines a concept of what the implementing class is.
For example, if you have a concrete class that extends an abstract
class, you're basically saying that the concrete class IS A(n) abstract
class. (A ktichen IS A room - for a real life example.) An interface,
on the other hand, is a contract for your class. So, if a class
implements an interface, it is essentially saying that that class will
have certain abilities and characteristics, but will provide its own
implementation of those things. So, a car and a plane and a truck are
all vehicles (abstract) but they all implement the Driving interface.
This guarantees they can all drive, but each one does it in their own
way.
So, back to your question.
An abstract has the ability to implement some of its methods while
leaving others up to the concrete classes. As a result, an abstract
class will need variables to implement those methods. What scope those
variables have to the concrete classes depends on what you want to do
with that abstract class. The two you should generally look at,
though, are private variables (the only way to access is via
getter/setter methods within the abstract class) and protected
variables. Protected variables can be directly accessed by classes
that extend the abstract class. In the following example,
AbstractClass implements a method called "stuff()." Stuff uses a
variable called "text." Because text is protected, the implementing
class, "ConcreteClass" can access "text" directly without worrying
about getters/setters.
public abstract class AbstractClass() {
protected String text;
public String stuff() {
text = "Some Stuff";
return text;
}
public abstract String otherStuff();
}
public class ConcreteClass() extends AbstractClass {
public String otherStuff() {
text = "Some Other Stuff"; // text variable from AbstractClass
return text;
}
}
I hope I got all that correct (it's early still) and I hope that
explains things a little bit for you. Someone correct me if I messed
up in my explanation anywhere.
> While writing abstract class do you define any variables?
> My question is: Why you do that? it doesn't make sense to me becouse
[quoted text clipped - 8 lines]
>
> THX
man4*.* - 17 Nov 2006 18:30 GMT
wow, THX a lot...you've mentioned a few very important
details that helped me a lot to figure out some other things...
THX 1 again!
trippy - 17 Nov 2006 23:56 GMT
> While writing abstract class do you define any variables?
> My question is: Why you do that? it doesn't make sense to me becouse
> you can't make instance of abstract class..
Because you might want the child class(es) to have those variables and
this way, you can ensure the child class(es) will have them. This
applies to methods too.
> I understand why to make non-abstract method in abstract class, but
> this story with variables...as I said doesn't make sense (most probably I'm
[quoted text clipped - 5 lines]
>
> THX

Signature
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "All I Really Want" -- Alanis Morissette
"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."
-- Robert Redford "Spy Game"