Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

syntax of an extend

syntax

  • Please log in to reply
8 replies to this topic

#1 RageArtworks

RageArtworks

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 24 April 2007 - 07:11 AM

I'm trying my hand at Java, but I don't know how this works.
Is it like:
class Alfabet {
class Vowel extends Alfabet { }
}

or:
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!
  • 0

#2 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 24 April 2007 - 07:56 AM

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;
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.
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!

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 RageArtworks

RageArtworks

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 24 April 2007 - 08:40 AM

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?
  • 0

#4 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 24 April 2007 - 09:18 AM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 24 April 2007 - 10:12 AM

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

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.
  • 0

#6 oubless

oubless

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 29 May 2007 - 01:34 AM

Extends:
class A{
...
}

class B extends A {
...
}
And inner class ( has nothing to do with extends ):
class A{
  class B{
  }
}
In this case B does NOT extends A, it just resides in A and is refered to as
A.B
new A.B();

  • 0

#7 Spitfire

Spitfire

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 11 June 2007 - 01:21 PM

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
  • 0

#8 oubless

oubless

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 13 June 2007 - 01:04 AM

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
  • 0

#9 Spitfire

Spitfire

    CC Lurker

  • Just Joined
  • Pip
  • 4 posts

Posted 13 June 2007 - 01:41 PM

That makes alot more sense :)
  • 0





Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download