Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-24-2007, 10:11 AM
RageArtworks RageArtworks is offline
Newbie
 
Join Date: Apr 2007
Posts: 4
Rep Power: 0
RageArtworks is on a distinguished road
Red face syntax of an extend

I'm trying my hand at Java, but I don't know how this works.
Is it like:
Code:
class Alfabet {
class Vowel extends Alfabet { }
}
or:
Code:
class Alfabet {
}
class Vowel extends Alfabet {
}
So like two seperate classes linked by the word extend, or is one class really inherited inside a class?

Forgive me if I sound stupid, but I'm doing the Java Tutorial, and I only got to this page java.sun.com/docs/books/tutorial/java/concepts/inheritance.html and it's cracking my mind. Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-24-2007, 10:56 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,414
Last Blog:
CherryPy(thon)
Rep Power: 27
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

If you do like your first example you'll declare a class inside the other class. I don't think it's possible to do like you first example, in Java, because you use the extends-keyword before the class is even declared and/or implemented.

The other example is possible, because you declares another class outside any others, and use the extends-keyword after the first class is declared.

In your second example the class Vowel is inherited from the class Alfabet. That means the Vowel have the same functionality as Alfabet - but you have the option to extend Vowel with more.

I'm not a Java-programmer, but C++ isn't much different (syntax), so you'll probably could understand my example;
Code:
class Base
{
	public:
		void specialFunction()
		{
			std::cout << "This is a special function!" << std::endl;
		}
		
		void anotherSpecialFunction()
		{
			std::cout << "This is another special function!" << std::endl;
		}
};

// Java: class Derived extends Base
class Derived : public Base
{
	public:
		void aVerySpecialFunction()
		{
			std::cout << "This is a very special function!!!" << std::endl;
		}
};
If making an instance of class Base you'll only get access specialFunction and anotherSpecialFunction. If you then want to use an extended version of the class, then you can use Derived, which have another special function, aVerySpecialFunction, but this class can also access the ones from Base.
Code:
Base bInst;
Derived dInst;

// This is the only two functions we can access with an instance of Base
bInst.specialFunction();
bInst.anotherSpecialFunction();

// But with an instance of Derived, we can access the same ones + one more
dInst.specialFunction();
dInst.anotherSpecialFunction();
dInst.aVerySpecialFunction(); // Another function!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2007, 11:40 AM
RageArtworks RageArtworks is offline
Newbie
 
Join Date: Apr 2007
Posts: 4
Rep Power: 0
RageArtworks is on a distinguished road
Default

That's what I thought, but the term Subclass confused me: I thought a subclass would be under (sub) another class, as in hierarchy.

Thanks a lot for the help.

PS.: You're 15? My god. When did you start programming?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-24-2007, 12:18 PM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,414
Last Blog:
CherryPy(thon)
Rep Power: 27
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

You're welcome!

Yes, I'm 15. I'm not a guru or anything, I just help as much as I can.
I've been programming for about 2-3 years. In the start mostly web-programming, but now my time goes on application-programming with C/C++ and Assembly. I have used C/C++ for about an year and a half, and Assembly only for a few weeks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-24-2007, 01:12 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,633
Last Blog:
Passwords
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default

You extend classes in the class header. For example, a class that would extend JFrame might look something like this:

Code:
package helloworld;

import javax.swing.*;

public class MySecondApp extends JFrame {
	
	public MySecondApp(){

	}
	public static void main(String[] args) {
		new MySecondApp();
	}
}
MySecondApp is a sub-class or child of the super-class/parent JFrame. It has the same ability of its parent, with the ability to add on (extend) the parents functionality with more methods. I've never attempted to extend classes within the same class file, but your second example does look OK.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-29-2007, 04:34 AM
oubless oubless is offline
Newbie
 
Join Date: May 2007
Posts: 22
Rep Power: 5
oubless is on a distinguished road
Default

Extends:
Code:
class A{
...
}

class B extends A {
...
}
And inner class ( has nothing to do with extends ):
Code:
class A{
  class B{
  }
}
In this case B does NOT extends A, it just resides in A and is refered to as
A.B
Code:
 new A.B();
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-11-2007, 04:21 PM
Spitfire Spitfire is offline
Newbie
 
Join Date: Jun 2007
Posts: 4
Rep Power: 0
Spitfire is on a distinguished road
Default

Question oubless, what use is there having a class inside the other class if they cant share methods and the like. Wouldnt it make more sense to just have them in seperate classes? Unless it was just to categorize... Alphabet.Vowel.method(); thats the only use i can really see for it. Sorry i have alot of daft questions :P
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-13-2007, 04:04 AM
oubless oubless is offline
Newbie
 
Join Date: May 2007
Posts: 22
Rep Power: 5
oubless is on a distinguished road
Default

Quote:
Originally Posted by Spitfire View Post
Question oubless, what use is there having a class inside the other class if they cant share methods and the like. Wouldnt it make more sense to just have them in seperate classes? Unless it was just to categorize... Alphabet.Vowel.method(); thats the only use i can really see for it. Sorry i have alot of daft questions :P
Read about inner classes - they are pretty strong as a concept...
You can use them to mimic multiple inheritance ( as you know it is not allowed in Java ). The inner class has access to ALL !!! fields and methods of the class it is nested in ( including private ones ).
I just googled those for you:
Java Tip 106: Static inner classes for fun and profit - Java World
Inner classes - Java World
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Inner Class Example (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Unfortunately there is no tutorial here about inner class ( or at least I didn't spotted any ) ... and as Sidewinder is author of most of them, try asking him to write one about inner classes. I just don't have enough time for this right now, sorry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-13-2007, 04:41 PM
Spitfire Spitfire is offline
Newbie
 
Join Date: Jun 2007
Posts: 4
Rep Power: 0
Spitfire is on a distinguished road
Default

That makes alot more sense
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
SQL syntax error reachpradeep Database & Database Programming 2 10-11-2007 11:29 AM
Finding Syntax Errors Sionofdarkness Python 1 01-06-2007 05:33 AM
Syntax Stuff Kaabi General Programming 3 08-21-2006 03:34 AM
Syntax Sionofdarkness C and C++ 7 07-23-2006 10:23 AM


All times are GMT -5. The time now is 03:54 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 65%

Ads