This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Anonymous inner class

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Anonymous class method is sizzle().But Anonymous class instance(myAccess) is not accepting sizzle() method.Why and how can I access sizzle() method.Please help me.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I see it you cannot. Because your anonymous inner class is a SUBCLASS of Popcorn and is being accessed by a reference of type Popcorn. So only methods defined in Popcorn (or its superclasses etc) can be accessed.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see the below code you will realize the things

class Popcorn {
public void pop() {

System.out.println("popcorn");
}

}

class MyInner{

//----------------------------outer class

Popcorn p=new Popcorn() {

//----------------------------anonymous inner class
public void sizzle() {

System.out.println("anonymous sizzing popcorn");

}
};

public void priya() {

(new Popcorn() {

//----------------------------anonymous inner class
public void sizzle() {

System.out.println("anonymous sizzing popcorn");

}
}).sizzle();
//---------------------------------will print Popcorn class pop()
}
}

class Objective15 {

public static void main(String[] args) {

MyInner outer=new MyInner();

outer.priya();

Popcorn myAccess=outer.p;

myAccess.pop();

// myAccess.sizzle();//--------------------------line1
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic