Jump to content

syntax of an extend

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
RageArtworks

RageArtworks

    Newbie

  • Members
  • Pip
  • 4 posts
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!

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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!


#3
RageArtworks

RageArtworks

    Newbie

  • Members
  • Pip
  • 4 posts
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?

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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.

#6
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts
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(); 


#7
Spitfire

Spitfire

    Newbie

  • Members
  • Pip
  • 4 posts
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

#8
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts

Spitfire said:

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

#9
Spitfire

Spitfire

    Newbie

  • Members
  • Pip
  • 4 posts
That makes alot more sense :)