Realm Master![]() Member Posts: 1971 From: USA Registered: 05-15-2005 |
I just don't get it. I don't see WHY in normal programming (well, not normal, maybe like a video game) or HOW you would use an abstract functoin. I know of interfaces, but I don't understand how they WORK. Like for instance, (if i can recall the correct name) a function like mouseWheelRoll(MouseWheel e) (made up off the top of my head, but i know there's stuff like it) is an abstract function, so you define it within the curly braces. What I'm wondering is how do you get it so that mouseWheelRoll(MouseWheel e) only functions when the mouse wheel is moved or rolled or stuff. I'm going to a 4th of july thing with my family, so I'll post more later. I'm thinking only a very limited amount of people are going to know what im talking about (Namely, HanClinto and Luke) -RM ------------------ Blessed are those who suffer for doing what is right. PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE! P.S. I HATE 640x480!!!!!! |
||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
i might be totally off, but if I remember correctly, abstract methods and classes needs to be inherited and defined. they only serve as a superclass. so you'd need to to define it yourself. interfaces is kind of a protocol that "says" your class has these certain features which can be accessed. so, if your class implements the Comparable interface, you're required to provide a couple methods, like compareTo() // i think, too lazy to check up on the api // that allow it to be compared. so anybody can look and see your class is Comparable and will know they can use the compareTo() method. you'll have to give the name of the class where your method is from. ------------------ |
||
luke![]() Member Posts: 311 From: I use your computer as my second Linux box Registered: 10-30-2005 |
Alrighty, I'd just LOVE to give you some pointers here lol #1 why have abstract methods/interfaces #2 How do said abstract methods get called?
Abstraction allows objects to 'react' in different ways that suit their needs/function. ------------------ [This message has been edited by luke (edited July 04, 2006).] [This message has been edited by luke (edited July 04, 2006).] [This message has been edited by luke (edited July 04, 2006).] |
||
Realm Master![]() Member Posts: 1971 From: USA Registered: 05-15-2005 |
O.O wow. Way more than i needed, and probalby way more than i understand, but thanks! I didn't know you knew java arch! Give me a few days, and i'll figure it out! thanks for the help! ------------------ Blessed are those who suffer for doing what is right. PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE! P.S. I HATE 640x480!!!!!! |
||
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
yeah, lotta Comp Sci classes are in java, so I took around 4 of them. Luke and Han are better, but if you need help, just ask. ------------------ |
||
jestermax![]() Member Posts: 1064 From: Ontario, Canada Registered: 06-21-2006 |
Its not just java arch; its object oriented design, java just happens to be pure object oriented. I can help if you want, i'm almost done my computer science program and i've tutored programming before (not boasting here, just letting you know). Abstract functions are a function with a name but no code. Which means if you're going to instantiate a class with one then you need to override that function. Interfaces allow you to use a design pattern called 'polymorphism'. This means that different objects act in a similar fashion. Example: It's a little difficult to grasp sometimes at first but the more you think about, the more you'll understand it. |
||
SSquared![]() Member Posts: 654 From: Pacific Northwest Registered: 03-22-2005 |
It just so happens Java implemented their events/actions as Interfaces, but an Interface does not really have anything to do with events. The event model described above is just one use of an Interface. Interfaces and abstract classes are an OO concept and are not necessarily Java specific. For example, an abstract class in C++ is created by having at LEAST one method with no implementation (pure virtual), but contains some methods WITH implementation. A Java interface is simply a class where ALL methods have NO implementation. So, an interface is really a class containing no implementation. An example of an abstract class may be an Animal class containing methods like: setColor - sets the animal's color You may look at setSound and setFood as being very specific to a given animal, so you may desire to have NO default implementation in the Animal base class. Your derived Zebra, Lion, and Seal will be REQUIRED to implement these methods. The other listed methods will have default implementation in the Animal class. An interface defines a set of behaviors you may want in your classes. It is often used to define similar behavioral ideas between completely non-related classes, but is by no means a requirement. Let's say you are creating a game and have many different game objects (enemy, player, crate, laser shot). You may want to create an interface which defines methods like collision, movement, physics, etc. Each of these objects implements this class, requiring EACH one to create their own version of these methods for THEIR OWN needs. Or perhaps you want to create a calculator and a color creator. You can create an interface with Add, Subtract, Divide, Multiply which take an integer value. The calculator will do its associated function and return the result value (3+4 returns 7, for example). The same is done for the color creator but the integer value may actually represent an RGB value. So, an interface is simply a way to define behavior you desire and FORCES you to IMPLEMENT EACH method in the interface. How do you access these? The same way you access any other method. If I have my calculator class which implemented Add, I will call calculator.Add(3, 4). Another purpose for Interfaces is to allow for Remoting. This, to me, is one of the coolest things. Remoting allows you to talk to a class running in a different process, or on a completely different machine. For remoting, the jave Interface defines the public access to the remote object. So, I can be running an application on my machine, and create a Calculator locally, but will actually access the calculator instance on steveth45's machine. The interface says, I can only talk to steveth45's calculator object using Add, Subtract, Multiply, and Divide. I do not have access to any other of Calculator's methods. One reason Sun chose to implement Interfaces how they did is to remove potential problems with multiple inheritance (something C++ has). Notice how in Java you can only derive from ONE class. If you want more behavior, anything else must be an interface you implement. Since an Interface requires you to implement EACH item, you will often have several methods in your class which you may actually never use. Sorry this is kind of long. |
||
luke![]() Member Posts: 311 From: I use your computer as my second Linux box Registered: 10-30-2005 |
True an interface doesn't really have anything to do with events; I was only using it as an example. SSquared is quite correct about the pure virtual function; in fact to make an interface following the characteristics of a Java interface, you could do this:
by just doing public pure virtual functins, you can make an interface(you can also have some consts in an interface too).
I only like the 'event' example because I just like event driven programming very much; interfaces allow so much more than just some simple 'event' driven programs. For example, say you have a class that does graphics but you update it often and you want earlier versions to benefit/not break from the upgrades in Java you could just make many different interfaces and have one class implement them all like
now when using this little interface practice that I have just demonstrated, as long as you maintain the same prototypes, your code will be backwards compatible. Microsoft has a standard called COM (Component Object Module or somehting like that I think) that is backwards compatible and works entirely through interfaces, and of which I know no more about. Fyi DirectX is COM formated. Please note that instead of using an interface your could just skip that and use a class but im just trying to explain some uses of Interfaces lol.
[This message has been edited by luke (edited July 05, 2006).] [This message has been edited by luke (edited July 05, 2006).] |
||
jestermax![]() Member Posts: 1064 From: Ontario, Canada Registered: 06-21-2006 |
inheritance is good if you're using it for the right reasons. theres a bazillion articles on the internet stating the reasons for that so i won't get into it now. Interfaces have a ton of nifty uses and are very good for keeping up good design. Yeah, you could easily get around without them but sometimes that can lead to poorly written code that noone can read. Cheers |