This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class" Watch "abstract class" New topic
Author

abstract class

Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
an abstract class cannot b instiated -- this is true
then in the code below- static A a1 = new A(2){....} -- how come holds.
pls clerefy.......
abstract class A {
private int x = 4;
private int y = 2;
public A(int i1) {x=i1;}
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
public void incY() {y++;}
public abstract int math();
}
class B {
static A a1 = new A(2) {
{incY();}
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(a1.math());
}
}


Mona(Varijasmom)
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
static A a1 = new A(2) {..
This looks like if you were creating an instance of A. But in reality, it's only a subclass of A. When you have an anonymous inner class, your either creating an on-the-fly subclass of a class or implementing an interface.
In this case, your anonymous inner class is implementing the abstract method from class A.
HTH


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: abstract class
 
Similar Threads
Anonymous class Dan's Mock exam
Dan's single topic-anonymous classes.
anonymous classes
dan's single topic exams-anonymous arrays
abstract class instantiation