everyone mistakes my question. here is a sample. I think there is NO answer. But thanks for all explainations of you. Thanks.///:Parent.java public class Parent { public void method (int i) { System.out.println("Parent.method(int)"); }} ///~ ///:Parent.java public class Parent { public void method (int i) { System.out.println("Parent.method(int)"); }} ///~
everyone mistakes my question. here is a sample. I think there is NO answer. But thanks for all explainations of you. Thanks.///:Parent.java public class Parent {public void method (int i) { System.out.println("Parent.method(int)"); }} ///~ ///:Son.javapublic class Son extends Parent { public void method (int i ) { System.out.println( "Son.method(int)"); } public static void main (String args[]) { Son s = new Son(); s.method( 1); //s.super.method(1); // with compiler error //(Parent)s.method(1); // with compiler error ((Parent)s).method(1); } } ///~ //// the program's result displays like these: // line 1: Son.method(int) // line 2: Son.method(int)//// a willing display is like // line 1: Parent.method(int)
you are right,it is overriden, so there is no way to call Parent class's (virtual) method with a Son instance
Thanks karma, but there may be some needs to do that, such as i want to print a String object's id rather than the string itself. and maybe other needs. what is the case in C++??
what is the case in C++??I means what is the right design for all object-oriented languages. and is there some docs for a design or logic of OO languages??
in C++, you can do:#include <iostream> using namespace std;class Parent { public: virtual void method (int i) { cout << "Parent.method(int)" << endl; }};class Son : public Parent { public: void method (int i ) { cout << "Son.method(int)" << endl; }};int main() { Son* son = new Son; son->method(1); son->Parent::method(1); return 1; } as for C++ language design, read "The Design and Evolution of C++" (Bjarne Stroustrup)
3ks, karma. I have other two questions related to above one.1. can you explain why Sun loses such a method call. maybe there is some reason.2. are there some books and docs about OO. because i want to discover a rule for OO language design. (maybe the book you mentioned is one of them)3ks again
1. because the runtime knows the exact type of a Son instance, whether you cast it to a Parent variable or not, so the Son's overridden method is always invoked2. That book is about the design of C++. If you want to learn OO design, here are three classics: a. Object-Oriented Analysis and Design with Applications ( 2nd edition) by Grady Booch b. Object-Oriented Modeling and Design by James Rumbaugh, Michael Blaha, William Premerlani, Frederick Eddy, William Lorenson c. Object-Oriented Software Engineering: A Use Case Driven Approach by Ivar Jacobson They are a little old and mostly theory-oriented and pretty hard to read. But almost every hard-code OO designer takes them as bibles
ok.1. i know the reason. but why not do some change ( work like c++)? 2. maybe one of them is on my shelf. (i will read it now)
1. you should ask SUN about that, :-). Anyway, Java is a more restrictive OO language, for some good reasons2. have fun!
public class Parent { public void method (int i) {
System.out.println("Parent.method(int)");
}} ///~
///:Parent.java
public class Parent { public void method (int i) {
System.out.println("Parent.method(int)");
}} ///~
everyone mistakes my question. here is a sample. I think there is NO answer. But thanks for all explainations of you. Thanks.///:Parent.java
public class Parent {public void method (int i) {
System.out.println("Parent.method(int)");
}} ///~
///:Son.javapublic class Son extends Parent {
public void method (int i ) {
System.out.println( "Son.method(int)");
}
public static void main (String args[]) {
Son s = new Son();
s.method( 1);
//s.super.method(1); // with compiler error
//(Parent)s.method(1); // with compiler error
((Parent)s).method(1);
}
} ///~
//// the program's result displays like these:
// line 1: Son.method(int)
// line 2: Son.method(int)//// a willing display is like
// line 1: Parent.method(int)
using namespace std;class Parent {
public:
virtual void method (int i) {
cout << "Parent.method(int)" << endl;
}};class Son : public Parent {
public:
void method (int i ) {
cout << "Son.method(int)" << endl;
}};int main()
{
Son* son = new Son;
son->method(1);
son->Parent::method(1);
return 1;
}
as for C++ language design, read "The Design and Evolution of C++" (Bjarne Stroustrup)
a. Object-Oriented Analysis and Design with Applications ( 2nd edition)
by Grady Booch
b. Object-Oriented Modeling and Design
by James Rumbaugh, Michael Blaha, William Premerlani, Frederick Eddy, William Lorenson
c. Object-Oriented Software Engineering: A Use Case Driven Approach
by Ivar Jacobson
They are a little old and mostly theory-oriented and pretty hard to read. But almost every hard-code OO designer takes them as bibles
2. maybe one of them is on my shelf. (i will read it now)