Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 12, 2007, 10:28 AM   #1
Jasonbot
macrumors 68020
 
Jasonbot's Avatar
 
Join Date: Aug 2006
Location: The Rainbow Nation RSA
Send a message via AIM to Jasonbot Send a message via MSN to Jasonbot
Java Classes

Hi guys, I'm writing my end of year java exam tomorrow and only heard today that I'm supposed to learn about using multiple classes in java. Could someone please show me in general how to use multiple classes?

Also, in classes does the same rule apply as in methods whereby variables can only be used with a single method unless they're static?

What is the purpose of having multiple classes?

When running a method from another class do I go class_name.method_name(); ?

Thanks guys.

On a side note, how does the 'extends' thing work with classes?

I found this on wikipedia:
Code:
public class Example1
{
    // This is a Java class, it automatically extends the class Object
    public static void main (String args[]) 
    {
        System.out.println("Hello world!");
    }
}

public class Example2 extends Example1
{
    // This is a class that extends the class created in Example 1.
    protected int data;
 
    public Example2()
    {
        // This is a constructor for the class.  It does not have a return type.
        data = 1;
    }
 
    public int getData()
    {
        return data;
    }
 
    public void setData(int d)
    {
        data = d;
    }
}
after the method Example2() runs does getData() run directly afterwards?
__________________
[insert apple related products here]

Last edited by Jasonbot; Nov 12, 2007 at 10:34 AM.
Jasonbot is offline   0 Reply With Quote
Old Nov 12, 2007, 10:48 AM   #2
pilotError
macrumors 68020
 
pilotError's Avatar
 
Join Date: Apr 2006
Location: Long Island
Short answer is you have to instantiate it and do something with it.

If you just compiled / ran the example you gave, it would just print hello world.

If you did something like:

Code:
Example2 myEx2 = new Example2();

myEx2.setData(5);

Sytem.out.println ("Data: " + myEx2.getData());
Essentially you would have to override the main() to do something with it, or use it as part of a method.

Your example is called inheritance and you use it to extend the functionality of a class.
pilotError is offline   0 Reply With Quote
Old Nov 12, 2007, 11:03 AM   #3
hankolerd
macrumors 6502
 
Join Date: Sep 2007
Location: Seattle, WA
Send a message via MSN to hankolerd
when you use the command "extend" it takes every function from example1, and carries it over to example 2, thus the main function in example1 is pulled into example2 class. But everything inside of example2 are functions, except for the first function which is called a constructor, which is how you initialize an example2 objects, much like you would say int i = new int(0); or int i = 0; which both do the same thing. you could say example2 bean = new example2(); bean.setdata(10);(or whatever that function was called).
The rest of the functions only run when called. Java only runs code inside the main function by default, by calling a function in main, you will go there, and execute the code in that function, and then return to main, unless you call another function from the first function call.

Hope this helps in some way.
__________________
20" iMac 2.16 C2D, 2GB RAM, 500GB HD, 256MB X1600
hankolerd is offline   0 Reply With Quote
Old Nov 12, 2007, 11:32 AM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by Jasonbot View Post
On a side note, how does the 'extends' thing work with classes?) runs does getData() run directly afterwards?
Conceptually it's called Polymorphism (link points to Java examples).
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 08:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 04:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 04:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 10:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 05:49 PM

Forum Jump

All times are GMT -5. The time now is 02:17 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC