The Artima Developer Community
Sponsored Link

Java Answers Forum
inner classes

9 replies on 1 page. Most recent reply: Apr 15, 2002 9:24 AM by Kishori Sharan

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 9 replies on 1 page
tejinder singh

Posts: 3
Nickname: tejisingh
Registered: Apr, 2002

inner classes Posted: Apr 14, 2002 4:40 AM
Reply to this message Reply
Advertisement
how can i make an object of class c .
class a
{
class b extends a
{
class c extends b
{
}
}
public static void main(String s[])
{
//how can i make an object of class c here
}
}
any help in this regard is highly appreciative
tejinder


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: inner classes Posted: Apr 14, 2002 11:50 AM
Reply to this message Reply
class a {
 
	class b extends a {
		class c extends b	{
			public void sayHello() {
				System.out.println ( "Hello from c" ) ;
			}
		}
	}
	
 
	public static void main(String s[]) {
	//how can i make an object of class c here
		a.b.c cc = new a().new b().new c() ;
		cc.sayHello() ;
		
	}
}

tejinder singh

Posts: 3
Nickname: tejisingh
Registered: Apr, 2002

Re: inner classes Posted: Apr 14, 2002 10:17 PM
Reply to this message Reply
kishori
try running your code .
tejinder

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: inner classes Posted: Apr 14, 2002 10:27 PM
Reply to this message Reply
the code works fine.... :)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: inner classes Posted: Apr 14, 2002 10:53 PM
Reply to this message Reply
Maybe what he should have said is try compling it:
a.java:18: No constructor matching a() found in class a.
        a.b.c cc = new a().new b().new c() ;
                   ^
a.java:18: No enclosing instance of class a is in scope; an explicit one must be
 provided when creating inner class a. b, as in "outer. new Inner()" or "outer.
super()".
        a.b.c cc = new a().new b().new c() ;
                          ^
2 errors

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: inner classes Posted: Apr 14, 2002 10:54 PM
Reply to this message Reply
compiling, that is!

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: inner classes Posted: Apr 14, 2002 11:00 PM
Reply to this message Reply
maybe it depends on what version of jdk you are using. I am using version 1.3 and do not get any of those *compilation* errors.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: inner classes Posted: Apr 15, 2002 12:34 AM
Reply to this message Reply
The inner classes do need an instance of the outter class to support themselves.
Unless for teachin' purpose I have almost never used inner classes extending the outter class as stated.

A solution is to mention the inner class to be static. IMHO, this is doing the opposit of what one is willing to do with inner classes!

Rgds,

Thomas,

tejinder singh

Posts: 3
Nickname: tejisingh
Registered: Apr, 2002

Re: inner classes Posted: Apr 15, 2002 6:08 AM
Reply to this message Reply
thanks for everybody's participation on this discussion.i am not able to understand the difference in compilation errors i.e when you compile with jdk1.2 u get the errors as given by matt but when u compile with jdk 1.3 u do not .why this difference is coming.
is there something different introduced in jdk1.3 with respect to inner classes
tejinder

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

JDK 1.2 Version Posted: Apr 15, 2002 9:24 AM
Reply to this message Reply
Hello Tejinder,
I am sorry I didn't mention JDK Version in my posting. It was JDK1.3 I used to compile my code. Here is the JDK1.2 version. See the changes in construtor of c and also you need to provide a default constructor in a.

Thanks
Kishori
/////////////////////a.java////////////
public class a {	
    // The following construtor is needed for JDK1.2
	a() {
	}
	public class b extends a {		
						
		public class c extends b	{		
			
			public c() {
				b.this.super() ; // New for JDK1.2
			}
			
			public void sayHello() {	
				System.out.println ( "Hello from c" ) ;		
			}	
		}	
		
	}		
 
	public static void main(String s[]) {	
		//how can i make an object of class c here		
		a.b.c cc = new a().new b().new c() ;	
		cc.sayHello() ;			
	}
}

Flat View: This topic has 9 replies on 1 page
Topic: passing object type as paramter to method from one call to another Previous Topic   Next Topic Topic: Array woes

Sponsored Links



Google
  Web Artima.com   

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