You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.2 KiB
116 lines
2.2 KiB
public class TestFruits
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
Apple a = new Apple(2);
|
|
System.out.println(Apple.count);
|
|
System.out.println();
|
|
|
|
Fruit[] fr = new Fruit[3];
|
|
fr[0] = a;
|
|
fr[1] = new RedDelicious();
|
|
fr[2] = new Fruit(4);
|
|
System.out.println();
|
|
|
|
System.out.println("Using array fr: ");
|
|
for(int i = 0; i < fr.length; i++)
|
|
{
|
|
fr[i].display();
|
|
System.out.println(Apple.count);
|
|
System.out.println();
|
|
}
|
|
}
|
|
}
|
|
|
|
class Fruit
|
|
{
|
|
public int f;
|
|
|
|
public Fruit(int f)
|
|
{
|
|
System.out.println("Fruit");
|
|
this.f = f;
|
|
}
|
|
|
|
public int getF()
|
|
{
|
|
return this.f;
|
|
}
|
|
|
|
public void f1()
|
|
{
|
|
System.out.println("FRUIT f1");
|
|
}
|
|
|
|
public void f2()
|
|
{
|
|
f1();
|
|
System.out.println("FRUIT f2");
|
|
}
|
|
|
|
public void display()
|
|
{
|
|
System.out.println("display method from Fruit invoked");
|
|
f2();
|
|
}
|
|
}
|
|
|
|
class Apple extends Fruit
|
|
{
|
|
public static int count = 0;
|
|
|
|
public Apple(int a)
|
|
{
|
|
super(a);
|
|
System.out.println("Apple");
|
|
count = count + getF();
|
|
}
|
|
|
|
public void f1()
|
|
{
|
|
System.out.println("Apple f1");
|
|
}
|
|
|
|
public void f2()
|
|
{
|
|
f1();
|
|
System.out.println("Apple f2");
|
|
super.f1();
|
|
}
|
|
|
|
public void display()
|
|
{
|
|
System.out.println("display method from Apple invoked");
|
|
f2();
|
|
}
|
|
}
|
|
|
|
class RedDelicious extends Apple
|
|
{
|
|
public static int count;
|
|
|
|
public RedDelicious()
|
|
{
|
|
super(3);
|
|
System.out.println("RedDelicious");
|
|
count = count + getF();
|
|
}
|
|
|
|
public void f1()
|
|
{
|
|
System.out.println("RedDelicious f1");
|
|
}
|
|
|
|
public void f2()
|
|
{
|
|
f1();
|
|
System.out.println("RedDelicious f2");
|
|
super.f1();
|
|
}
|
|
|
|
public void display()
|
|
{
|
|
System.out.println("display method from RedDelicious invoked");
|
|
f1();
|
|
}
|
|
}
|