aspose file tools*
The moose likes Beginning Java and the fly likes About Inheritance in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Inheritance in JAVA" Watch "About Inheritance in JAVA" New topic
Author

About Inheritance in JAVA

Sandeej Kottal
Greenhorn

Joined: Nov 12, 2000
Posts: 2
I would like to know more in depth about Inheritane as Aim new o Object Oriented programming.
Hope to get good repsonse..


Every day I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work. " <br />- Robert Orben
Brett Knapik
Ranch Hand

Joined: Oct 15, 2000
Posts: 255
If you check out the campfire & the code barn of javaranch you can get an excalamation on multiple inheritence.
for starters you get inheritence by:
class dog extends animal {
//class contents
}
for multiple inheritence use
class dog extends animal implements home, cute {
//class contents here
}

------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Originally posted by Sandeej Kottal:
I would like to know more in depth about Inheritane as Aim new o Object Oriented programming.
Hope to get good repsonse..

Sandeej, here are a few excerpts from my book (Chapter 5, to be exact) on the subject of inheritance:
"Inheritance is a powerful OOP mechanism through which we can derive new classes by describing only how they differ from existing classes.
Let's assume that we have accurately and thoroughly modeled all of the essential features of students via a Student class [...]. A simplified version of the Student class is shown below.
class Student {
private String name;
private String studentId;
// etc.
public String getName() {
return name;
}
public void setName(String newName) {
name = newname;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String newId) {
studentId = newId;
}
// etc.
}

In fact, let's further assume that our Student class code has been rigorously tested, found to be bug free, and is actually being used in a number of applications: our Student Registration System, for example, as well as perhaps a student billing system and an alumni relations system for the same university.
A new requirement has just arisen for modeling graduate students as a special type of student. As it turns out, the only features of a graduate student that we need to track above and beyond those that we've already modeled for a 'generic' student are:
o What undergraduate degree the student previously received before entering his/her graduate program of study, and
o What institution he/she received the undergraduate degree from.
All of the other features necessary to describe a graduate student - attributes name, studentId, and so forth, along with all of the methods to manipulate these - are the same as those that we've already programmed for the Student class, because a graduate student is a student, after all.
By use of the Java reserved word 'extends', we could therefore define a Graduate Student as a special type of Student, having two extra attributes - undergraduateDegree and undergraduateInstitution - that 'generic' Students do not have a need for, as shown below:
class GraduateStudent extends Student {
// Declare two new attributes above and beyond
// what the Student class declares ...
private String undergraduateDegree;
private String undergraduateInstitution;
// ... and a pair of accessor/modifier methods
// for each of the new attributes ...
public String getUndergraduateDegree() {
return undergraduateDegree;
}
public void setUndergraduateDegree(String u) {
undergraduateDegree = u;
}
public String getUndergraduateInstitution() {
return undergraduateInstitution;
}
public void setUndergraduateInstitution(String u) {
undergraduateInstitution = u;
}
}
That's all we need to declare in our new GraduateStudent class: two attributes plus their get/set methods! There is no need to duplicate any of the attributes or methods of the Student class, because we are automatically inheriting these. It is as if we had 'plagiarized' the code for these attributes and methods from the Student class, and inserted it into GraduateStudent, but without the fuss of actually having done so.
(There are a few extra complexities about inheriting private features which I am temporarily 'glossing over' -- the 'protected' visibility type comes into play, and I discuss that in Chapter 13 of the book.)
[...]
Inheritance is perhaps one of the most powerful and unique aspects of an OO programming language because:
o Derived classes (e.g., GraduateStudent) are much more succinct than they would be without inheritance. Derived classes only contain the 'essence' of what makes them different from their ancestor class(es). We know from looking at the GraduateStudent class definition, for example, that a graduate student is 'a student who already holds an undergraduate degree from an educational institution'. As a result, the total body of code for a given application is significantly reduced as compared with the traditional non-OO approach to developing the same application.
o Through inheritance, we can reuse and extend code that has already been thoroughly tested without modifying it. As we saw, we were able to invent a new class - GraduateStudent - without disturbing the Student class code in any way. So, we can rest assured that any client code that relies on instantiating Student objects and passing messages to them will be unaffected by the creation of subclass GraduateStudent, and thus we avoid having to retest huge portions of our existing application. (Had we used a non-OO approach of 'tinkering' with the Student class code to try to accommodate graduate student attributes, we would have had to retest our entire existing application to make sure that nothing had 'broken'!)
o Best of all, you can derive a new subclass from an existing class even if you don't own the source code for the latter! As long as you have the compiled version of a class, the inheritance mechanism works just fine; you don't need the original source code of a class in order to extend it. This is one of the significant ways to achieve productivity with an object-oriented language: find a built-in class that does much of what you need, and create a subclass of that class, adding just those features that you need for your own purposes � or, buy a third party library of classes written by someone else, and do the same.
q Finally, as we saw in Chapter 2, classification is the natural way that humans organize information; so, it only makes sense that we'd organize our software along the same lines, making it much more intuitive and hence easier to maintain and extend.
(end of book excerpt)"
I hope this helps! Let me know if you'd like more detail.
------------------
author of:
Beginning Java Objects


Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
Originally posted by Brett Knapik:
If you check out the campfire & the code barn of javaranch you can get an excalamation on multiple inheritence.
for starters you get inheritence by:
class dog extends animal {
//class contents
}
for multiple inheritence use
class dog extends animal implements home, cute {
//class contents here
}


Brett, be careful with your terminology: implementing an interface is not the same thing as inheritance. When we implement an interface, we're simply forcing the implementing class to provide method bodies for all of the method signatures that the interface calls for; only with true inheritance do we have the potential to actually inherit behaviors. Implementing an interface is a way of telling a class "what" it needs to be able to do without telling it HOW to do those things.
It is correct, however, that since Java does not support true multiple inheritance -- i.e., one class being able to have two or more immediate parent classes, such as in the following INCORRECT class definition:
class Animal { }
class Pet { }
class Dog extends Animal, Pet { } // NOT ALLOWED IN JAVA!
-- that being able to implement multiple interfaces is the next best thing.
Regards,
Jacquie
------------------
author of:
Beginning Java Objects
Brett Knapik
Ranch Hand

Joined: Oct 15, 2000
Posts: 255
ok, thanks
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: About Inheritance in JAVA
 
Similar Threads
rent this movie: Rushmore
Shivaji - To be released
Size of Text Field
Some thoughts on "In to the West"
How to get dynamic content in index.jsp page