• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract class

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
abstract class A {
private int x = 4, y = 2;
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 abstract int math();
}
class B {
static A a1 = new A() {public int math() {return x()+y();}};
static A a2 = new A() {public int math() {return x()-y();}};
static A a3 = new A() {public int math() {return x()*y();}};
static A a4 = new A() {public int math() {return x()/y();}};
public static void main(String[] args) {
System.out.print("" + a1.math() + a2.math() +
a3.math() + a4.math());
}}


can anyone explain the flow plz
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Check this thread.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I hope this is correct.

1)Class A is abstract class which contains one abstract instance method math().
2)in Class B,4 static objects of class A are instantiated.
3)and for each object the abstarct math method is overridden differently.
static A a1 = new A() {
public int math() {
return x()+y(); // will return 4 + 2 = 6
}
};
static A a2 = new A() {
public int math() {
return x()-y(); // will return 4 - 2 = 2
}
};
static A a3 = new A() {
public int math() {
return x()*y(); // will return 4 * 2 = 8
}
};
static A a4 = new A() {
public int math() {
return x()/y(); // will return 4 / 2 = 2
}
};
4)print statement will give the o/p 6282


Cheers
Maady
 
reply
    Bookmark Topic Watch Topic
  • New Topic