Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 03-23-2006, 05:47 PM   #1 (permalink)
ice
Registered User
 
Join Date: Mar 2006
Location: Malaysia
Posts: 7
ice is on a distinguished road
help me.. how to apply inheritance&polymorphsm in GUI

hello,

all i know, GUI is only an interface..
what code should i use/add/change to implement inheritance& polymorphsm in GUI?
can i use same way to implement linkedlist in GUI?

thank you..
ice is offline   Reply With Quote
Old 03-24-2006, 01:59 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
All inheiritence is is when you extend a class and add some functionality. It's very straight forward - simply use the "extends" keyword when defining a class. It's covered under Learning the Java Language.

Polymorphism is a very simple concept that I can't remember the text-book definition of right now. Basically you start to use it naturally and don't think of it as ploymorphism. I have to run but when I get the chance I'll look up what it means and give you a few examples.
__________________
GitS

Last edited by Belisarius; 03-24-2006 at 12:26 PM.
Belisarius is offline   Reply With Quote
Old 03-24-2006, 12:25 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
Polymorphism is basically accessing a property of an object without knowing exactly what object that is. The most basic example in Java would be the "toString()" method implemented by all Objects. Take this code for instance (note, this isn't really legal Java code, it's just enough to illustrate the point):

Code:
// Define some example classes

class A {
  public String toString(){
    return "foo";
  }
}

class B {
  public String toString(){
    return "bar";
  }
}

// Define a method in some class

public void print(Object test){
  System.out.println(test);
}

// Put this in your main method

A a = new A();
B b = new B();

print(a);
Let's look at what is going on. First, look at the "print()" method. We told it to accept all objects of type "Object". Well, in Java, all objects are implicitly (that is they are by default) Objects in addition to their own types. That means A is an "Object" and B is and "Object". That also means that they can call all methods defined for "Object", one of which is "toString()".

Now, we wanted the "toString()" method to work differently in A than how it works in "Object" or B. So we "overrode" it, or rewrote it. This new "toString()" method only effects objects of type "A". Same goes for the "toString()" method defined in "B". But, because it was defined in the parent class "Object", Java knows it will *always* be defined. If we hadn't written a "toString()" method in "A" and "B", then it would have simply inheirited the "toString()" method written for "Object". Because Java knows that method will always be there, in our "print()" method, we can safely call it no matter what kind of "Object" we pass to it.

Now, you might be wondering how Java knows that the Object that we pass to "print()" is an A or B object, and which "toString()" method to call. Java keeps track of this all internally so you don't have to. If you tell a method to accept a certain object, it will accept not only that object, but all of its children (classes that inheirt/extend it) as well. However, only those methods and properties defined in the parent class (in this case, Object) will be accessible. That is, if we defined a "exampleMethod()" class in "A", in "print()" we could not say "test.exampleMethod()". We would have to "cast" the "test" object to "A", but that is outside the scope of this discussion.

To summarize, polymorphism (in Java at any rate) is the ability to access properties of a given object by calling on what is defined in the parent class without knowing exactly what the child class is.
__________________
GitS

Last edited by Belisarius; 03-25-2006 at 04:33 AM.
Belisarius is offline   Reply With Quote
Old 03-24-2006, 08:42 PM   #4 (permalink)
destin
Java Junkie
 
destin's Avatar
 
Join Date: Mar 2005
Posts: 40
destin is on a distinguished road
Quote:
Originally Posted by Belisarius
Code:
// Define some example classes

class A {
  public String toString(){
    return "foo";
  }
}

class B {
  public String toString(){
    return "bar";
  }
}

// Define a method in some class

public void print(Object test){
  System.out.println(test);
}

// Put this in your main method

A a = new A();
B b = new B();

print(A);
In Java 5, there is annotation to override methods:
Code:
class A {
    @Override public String toString() {
        return "foo";
    }
}
Personally, I like this. It lets you know when you are actually overriding a method. Prior to this, you would write out the method and wouldn't be sure if you were overriding the method. If, for example, you write toStirng() instead of toString(), the code would compile, but not run as expected. With the override annotation you will receive an error telling you that there is no method toStirng() to override.

@Belisarius
I think you meant that last bit to be print(a);, rather than print(A);.
destin is offline   Reply With Quote
Old 03-25-2006, 04:34 AM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
You're right, I corrected the code. Just a typo. And I didn't realize that was a change in 1.5, although it should accept the old way as it's suppose to be backwards compatable.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 03-25-2006, 04:40 AM   #6 (permalink)
destin
Java Junkie
 
destin's Avatar
 
Join Date: Mar 2005
Posts: 40
destin is on a distinguished road
Quote:
Originally Posted by Belisarius
And I didn't realize that was a change in 1.5, although it should accept the old way as it's suppose to be backwards compatable.
It does accept the old way; I was just pointing this out.
destin is offline   Reply With Quote
Old 03-26-2006, 07:08 PM   #7 (permalink)
ice
Registered User
 
Join Date: Mar 2006
Location: Malaysia
Posts: 7
ice is on a distinguished road
Quote:
Originally Posted by Belisarius
All inheiritence is is when you extend a class and add some functionality. It's very straight forward - simply use the "extends" keyword when defining a class. It's covered under Learning the Java Language.

Polymorphism is a very simple concept that I can't remember the text-book definition of right now. Basically you start to use it naturally and don't think of it as ploymorphism. I have to run but when I get the chance I'll look up what it means and give you a few examples.


i did learn polymorphsm but i do not know how to apply it in interface(GUI) that i created.
for example, i create an interface (GUI) for user to enter his personal data, his D.O.B and address. my program allow many users to enter their data, how can i ensure that i can store the previous data and retrieve anyone's data?


thank you..
ice is offline   Reply With Quote
Old 03-27-2006, 12:27 PM   #8 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
That's not inheirently a polymorphism problem, but you could turn it into one by saying that there are different types of users.

The simplest way to store the information would be to create an object to store all the properties. For example, create a Person class with variables for the DOB, address, etc. You can then write the object to a file in order to store it and read the file in order to load it back up. A simple way to do this would be to use Serialization.

To showcase ploymorphism, you could create different users, such as, say, an InetPerson, which would contain all the information to contact them online - it would be an extension of the Person object.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GUI Screen Scraping - How ? harry569 Platform/API C++ 8 04-17-2008 01:57 PM
GUI web Program comsa Linux / BSD / OS X 1 04-24-2005 11:22 PM
GUI Frontend Tutorial? Arker Standard C, C++ 1 05-30-2003 04:13 PM
C++ GUI Toolkist and Books WinterWolf Standard C, C++ 15 04-08-2003 10:14 AM
c++ gui with db connect sde Standard C, C++ 3 09-12-2002 12:22 PM


All times are GMT -8. The time now is 01:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting