The Artima Developer Community
Sponsored Link

Java Answers Forum
Polymorphism Questions

5 replies on 1 page. Most recent reply: Mar 7, 2004 10:22 PM by mudit

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Polymorphism Questions Posted: Mar 5, 2004 12:14 AM
Reply to this message Reply
Advertisement
Probably very simple stuff for most of you but I'm learning it and I still have some questions:

1) Is there any way to call BaseClass.method2 from bc or dc (defined below)?

BaseClass bc = new DerivedClass();
DerivedClass dc = new DerivedClass();

Is the only way to call "super" from within the DerivedClass?

2) In terms of early vs late binding: it certainly seems like the doSomething methods use early binding. The "Thinking In Java" book says "All method binding in Java uses late binding unless the method is static or final (private methods are implicitly final)." However, neither of the 2 methods I'm referring to are static or final.

----------------------------
OUTPUT:

BaseClass.method1
DerivedClass.me thod2
DerivedClass.method2
----------
Do something with baseclass
Do something with derived class
-----------------------------
PROGRAM:
class BaseClass {
	void method1() {
		System.out.println("BaseClass.method1");
		method2();
	}
	
	void method2() {
		System.out.println("BaseClass.method2");
	}
}
 
class DerivedClass extends BaseClass {
	void method2() {
		System.out.println("DerivedClass.method2");
	}
}
 
 
class InheiritTests {
	public static void doSomething(BaseClass base) {
		System.out.println("Do something with baseclass");
	}
	
	public static void doSomething(DerivedClass derived) {
		System.out.println("Do something with derived class");
	}
 
	
	public static void main (String args[]) {
		BaseClass bc = new DerivedClass();
		DerivedClass dc = new DerivedClass();
		
		bc.method1();
		bc.method2();
		System.out.println("----------");
		doSomething(bc);
		doSomething(dc);
		
	}
}

------------------------------


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Polymorphism Questions Posted: Mar 5, 2004 5:54 AM
Reply to this message Reply
> Probably very simple stuff for most of you but I'm
> learning it and I still have some questions:
>
> 1) Is there any way to call BaseClass.method2 from bc or
> dc (defined below)?
>
> BaseClass bc = new DerivedClass();
> DerivedClass dc = new DerivedClass();

I'm pretty sure that the answer is NO. Since both bc and dc are DerivedClass objects, the method2 that applies is the one in DerivedClass. If you create a BaseClass object -
BaseClass base = new BaseClass();
- then you can call BaseClass.method2 from it.

If you want to be able to access a method in BaseClass from DerivedClass, don't override it. Either give the method in DerivedClass a different name, or a different parameter list.

> Is the only way to call "super" from within the
> DerivedClass?

I believe so.

Sorry, but I can't help with the late binding vs. early binding stuff.

Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Re: Polymorphism Questions Posted: Mar 5, 2004 9:23 AM
Reply to this message Reply
BaseClass bc = new DerivedClass();


So, is a fair way to describe the above statement that "bc is a DerivedClass object with a BaseClass interface but can be downcasted"?

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Polymorphism Questions Posted: Mar 5, 2004 4:41 PM
Reply to this message Reply
>
> BaseClass bc = new DerivedClass();
> 

>
> So, is a fair way to describe the above statement that "bc
> is a DerivedClass object with a BaseClass interface but
> can be downcasted"?


No. bc is a DerivedClass object. But DerivedClass objects are also BaseClass objects is much the same way as Cats are Mammals. You are dealing with inheritance, not interfaces here. (Although I can see why you'd mix the two up.)

Chris Mountford

Posts: 1
Nickname: christo
Registered: Mar, 2004

Re: Polymorphism Questions Posted: Mar 7, 2004 7:04 PM
Reply to this message Reply
> >
> > BaseClass bc = new DerivedClass();
> > 

> >
> > So, is a fair way to describe the above statement that
> "bc
> > is a DerivedClass object with a BaseClass interface but
> > can be downcasted"?
>
>
> No. bc is a DerivedClass object. But DerivedClass
> objects are also BaseClass objects is much the same way as
> Cats are Mammals. You are dealing with inheritance, not
> interfaces here. (Although I can see why you'd mix the
> two up.)


I see what you're saying, and you're correct - but the term "interface" may be used more generically than the java interface type. When the bc variable is declared to be of type BaseClass, its contract is to adhere to the BaseClass interface, or the set of method signatures for the BaseClass.

Although we know that this bc is actually a DerivedClass instance, you can't invoke any DerivedClass methods that are not also declared in BaseClass without first casting it to DerivedClass and thereby not invoking the method on dc, but on the cast or a new variable declared as a DerivedClass. Unfortunately there are no exclusively DerivedClass methods in this example.

In summary, I believe it is correct to say, as Kevin asked, "bc is a DerivedClass object with a BaseClass interface but can be downcasted" because the term interface is clearly used as a description of the set of defined BaseClass method signatures, rather than confusing the concrete BaseClass with a literal java interface.

mudit

Posts: 10
Nickname: mbhandari
Registered: Feb, 2004

Re: Polymorphism Questions Posted: Mar 7, 2004 10:22 PM
Reply to this message Reply
Please any body who can send some more questions on Plymorphisim

Flat View: This topic has 5 replies on 1 page
Topic: NYC Study Group Anyone? Previous Topic   Next Topic Topic: Package

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use