Southperry.net
Couple Of Programming Questions - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: Couple Of Programming Questions (/showthread.php?tid=10466)



Couple Of Programming Questions - Conciente - 2009-04-21

Hello,

I have a couple of questions: (Java related)

1. What is an interface? What is an abstract class? What are their differences and when is it better to use one or the other?

2. What is the Static attribute? Define it when it is used on a Method. When is it convenient to use the Static attribute?


Couple Of Programming Questions - Thunda - 2009-04-22

Interfaces are like "classes", however, they are composed of only abstract methods. Abstract classes contain at least one abstract method. However, the advantage of an interface is that multiple interfaces can be inherited. Abstract classes, however, can provide concrete methods that do not have to be supplied by the subclass.

A static method or field is one that does not need the class object to be created, to be used. It is useful when you do not want to take up more space with objects.


Couple Of Programming Questions - Morgana - 2009-04-22

Static methods are also useful when the operation is always the same, and you can't be bothered to create a new object all the time.

For example, Java.lang.Math.random() (or wherever the Math class is; I always have to look it up) is a static method. You wouldn't want to have to create a "Math" class every time you use it... Which would have fairly useless properties anyway.


Couple Of Programming Questions - Spaz - 2009-04-22

Conciente Wrote:Hello,

I have a couple of questions: (Java related)

1. What is an interface? What is an abstract class? What are their differences and when is it better to use one or the other?

2. What is the Static attribute? Define it when it is used on a Method. When is it convenient to use the Static attribute?

To add to what the others have said, use an abstract class when you want to provide implementation of some methods, use an interface when you don't.

Define a method as static when it doesn't make sense for it be associated with an object, ie it doesn't use any of the object's state. If you have a lot of static methods, it's a sign you may be thinking too procedurally.


Couple Of Programming Questions - Nikkey - 2009-04-22

Thunda Wrote:Interfaces are like "classes", however, they are composed of only abstract methods. Abstract classes contain at least one abstract method. However, the advantage of an interface is that multiple interfaces can be inherited. Abstract classes, however, can provide concrete methods that do not have to be supplied by the subclass.

A static method or field is one that does not need the class object to be created, to be used. It is useful when you do not want to take up more space with objects.

Do abstract classes have to contain at least one abstract method?

Anyway, an abstract class is a class you can't create, but you're allowed to make subclasses of (extended classes).

As an example, let's say we have an abstract class Car. You cannot create a Car, because you must specify what kind of car it is. The abstract class can also have an extended abstract class as well. Say, we want to add in Mazda under the class Car. We cannot create a Mazda either, because we still need to specify what kind of Mazda we want. If we finally make a class MazdaMX_5, and create an instance of this, we're ready to go.

The Car class may contain special variables which a car needs, functions, and abstract functions. Say we have the function GearUp, which gears the current car one gear up. This function is abstract, as all cars doesn't gear in the same way, and all cars doesn't contain the same amount of gears. Thus this must be specified within a special type of Car, or a special brand, if all their cars have the same gear-mechanism.

A static method is a function which doesn't need to create instances of its element in order to do something. Say, Math.pow(x, y) for example. It's when you don't need more than the input-variables and constants to compute the value, or to do what it is ordered to do.


Morgana Wrote:Static methods are also useful when the operation is always the same, and you can't be bothered to create a new object all the time.

For example, Java.lang.Math.random() (or wherever the Math class is; I always have to look it up) is a static method. You wouldn't want to have to create a "Math" class every time you use it... Which would have fairly useless properties anyway.
Hmm, I never really though about that Math.random() was a static function. Funnily enough though, it creates a pseudorandom-generator and stores it as a static object within itself... wat.

Code:
private static Random randomNumberGenerator;

    private static synchronized void initRNG() {
        if (randomNumberGenerator == null)
            randomNumberGenerator = new Random();
    }

    public static double random() {
        if (randomNumberGenerator == null) initRNG();
        return randomNumberGenerator.nextDouble();
    }

Talking about... doesn't compute within my head. wahh.


Couple Of Programming Questions - GummyBear - 2009-04-22

Your question relates to inheritance and polymorphism, and of course design patterns.

An interface is a declaration. It says what uses this interface should have. Eg a car should be drivable, ie function drive(). How is another story. Practical usage of interfaces generally lies in adaptation. It however can be used to build an abstract hierachy, and one of the biggest hierachy is the java library or C++ stl.

An abstract class is a class that cannot be instantiated (as mentioned above). Abstract classes sometimes define some functionalities, while leaves others. It can be used to define part of a function, which is common to all child classes. This is sometimes reflected via parent::function (syntax variation by languages). Abstract classes are closer to home when it comes on inheriting than interfaces. They tend to be used widely for extensible definitions.

A static attribute is an attribute that can be referred directly without instantiating the required object. It is a shared space between all instances, and updating its value affects all instances. A simple use of a static variable is to keep track of the number of objects created.

Further more, you can also have static functions. While most are just plainly forced, eg java public static void main, due to the way java works, some has deeper values. Two design patterns that relies on static functions are the factory pattern and the singleton pattern (this pattern requires static function).