Class/abstract methods
I am new to java and also to programming, so I am struggling with a few oop concepts. These include class methods and anything having to do with abstract methods (the methods themselves, interfaces, etc...).
I understand class and instance variables, but I don't quite understand class and instance methods. I understand that each object receives it's own copy of the method if it is an instance method, and that they share a copy if it is declared static. But I don't understand what this does. Are static methods used to refer to static variables? I just don't quite understand the differences in behavior between the two.
I am also having trouble understanding abstract methods. Since the abstract method contains only the signature and not the body, it seems like a waste to have to define the entire body it anywhere you want to use it.
On a side note, does declaring a class abstract provide a speed benefit? Or is it just to prevent someone from using a class they shouldn't?
Thanks for reading through,
Desro
[1081 byte] By [
Desro] at [2007-11-11 7:20:02]

# 1 Re: Class/abstract methods
- abstract classes are for design requirements.
you may have a method which is not meaningful in base class.
but you want all derived classes should implement it.
without abstract classes , you should put non-sense or empty functions in base classes.
a non-sense example for programming
but its useful for understanding concept.
class Animal has a method move();
class Bird move() as flying.
Class Snake move() as creeping.
but how move() an Animal ? it shouıld be abstract.
-every instance has not a copy of instance functions.
but instance functions have a hidden parameter. ( "this" )
so when an instance call a instance method you can use "this" keyword inside function. ( you can use it explicitly or implicitly )
"this.a" is same as "a" ( if a is an instance variable. )
so you are right, that instance methods are for modifying instance variables.
( instances has their own copy of each instance variables .. )
you are right with static methods too.
they has not that "this" hidden parameter. they can not reach instance variables they only can modify static variables ( class variables )
mr1yh1 at 2007-11-11 22:38:45 >

# 2 Re: Class/abstract methods
Thanks! That cleared a few things up.
So the main difference between static and non static methods is the "this" keyword? Also, could you refer to an instance variable from a static method like this: instanceName.variableName
Or could you refer to a class variable from an instance method like this: className.variableName
Also, I do not understand object casting at all.
Thanks for your help. :)
Desro at 2007-11-11 22:39:40 >

# 3 Re: Class/abstract methods
yes you can do that things.
forexample:
if class A has a method
static void foo( A caller ) ,
you can do everything inside foo,
as if it was a non-static method, becos you can send caller information.
A a = new A();
A.foo(a); // you can reach instance variables via a.
but if you need "this", you should use instance methods.
you will see difference in inherited methods.
mr1yh1 at 2007-11-11 22:40:43 >

# 4 Re: Class/abstract methods
i forgot last question..casting means type conversion, but only if they are compatible.
forexample
"class SportCar extends Car" means , SportCar is a kind of Car.
Car car = new Car();
SportCar sportCar = new SportCar();
Car c = (Car) sportCar; //legal
you can call a sport car as a car.
SportCar sc = (SportCar) c ;// legal
becos its still a sport car.
SportCar sc2 = (SportCar) car ; // illegal
every car is not a sport car.
mr1yh1 at 2007-11-11 22:41:43 >

# 5 Re: Class/abstract methods
One more question about the "this" keyword. Does "this" refer to the object itself or to the reference to the object? The reason I ask is because I am trying to make a sort of "deleteObject" method. Could I say something like this = null;
Thanks for your replies so far. :)
Desro at 2007-11-11 22:42:41 >

# 6 Re: Class/abstract methods
java didnt let it.
"this = null" give an compile error.
saying , its defined a final variable in a class.
(anyway, it would be a referance. )
when you use ordinary java objects, mostly you dont need memory management. ( you may need it , if you have an object which hold memory for other objects .. )
if any other object dont reference your object.
your object will be a candidate for being garbage collected .
also you may use :
yourObject = null;
outside of your class.
mr1yh1 at 2007-11-11 22:43:51 >
