|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 { }
}
Code:
class Alfabet {
}
class Vowel extends Alfabet {
}
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! |
| Sponsored Links |
|
|
|
|||||
|
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;
}
};
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!
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
|
|||||
|
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum C/C++ resources - C/C++ frequently asked questions Python resources - Python frequently asked questions I'm always up for a chat, so feel free to contact me... |
| Sponsored Links |
|
|
|
|||
|
Extends:
Code:
class A{
...
}
class B extends A {
...
}
Code:
class A{
class B{
}
}
A.B Code:
new A.B(); |
|
|||
|
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
|
|
|||
|
Quote:
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 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| John | ........ | 223.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| gaylo565 | ........ | 18.00000 |
| WingedPanther | ........ | 15.00000 |
| |pH| | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 65%