![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
Extending Classes in Java
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 18,715
Join Date: Jan 2006
Rep Power: 10
IM:
|
What this means is that when a class extends to another, the class that receives the extension can use all methods and constructors of the class that extends. For a class to receive an extension you must use the following class header: public class ClassName extends ClassExtender First to establish some terminology, the ClassName is a sub class and the ClassExtender is a super class. In java, unlike C programming, a class can receive only one extension from another class. In the example, ClassName can access all of ClassExtender's constructors and methods in this case. If ClassExtender had a method called q, an instance (object) of ClassName could execute the process. It becomes more interesting if ClassName also had a method of the name q. If you called a method in with both the super class and the sub class, the sub class' method would execute. The opposite situation, where the sub class contains the method and the super class doesn't is complicated also. If a sub class object was cast into the subclass variable, it would be able to execute the method specific only to the subclass. But if the object was of a subclass cast to a super class, the object couldn't execute the classes method, Case 1 ClassName objectQ = new ClassName(); aMethod(); // aMethod is a method only to the ClassName class // This would work fine Case 2 ClassExtender objectQ = new ClassName(); aMethod(); // aMethod is a method only to the ClassName class // This would NOT work To fix this, use the following ClassExtender objectQ = new ClassName(); ((ClassName)objectQ). aMethod(); // aMethod is a method only to the ClassName class // This would work If you want to access a super class' method, its simply super.method() Also, when you create a constructor of a sub class, you should always have a line at the top of the constructor with the following. super(); |
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extending the Windows Vista Trial Period | Anilrgowda | Microsoft windows vista error | 0 | 15-Aug-2007 11:09 PM |
| Extending Windows 2003 Partition | ps3cheats | Microsoft windows 2003 error | 1 | 15-Mar-2007 01:47 AM |
| The Javalog.txt file is created in the Windows\Java folder when Java logging is enabl | Anilrgowda | Microsoft windows vista error | 0 | 29-Jan-2007 10:09 AM |
| Java: Wrapper Classes | Admin | Programming tutorials | 0 | 03-Dec-2006 11:34 PM |
| C++: Template Classes | Admin | Programming tutorials | 0 | 03-Dec-2006 11:23 PM |