A question was asked to me which is as follow:

There is a class Fruitlist which has a sub class apple
and there is a class Fruit which has a sub class orange

FruitList uses Fruit (* not sub classed, only uses it)
and
Apple uses Orange


Is there something wrong in above scenario?
or
writing a java code for above scenario can be possible?

Edited 6 Years Ago by Little Bunny: n/a

Hmmm..

"There is a class Fruitlist which has a sub class apple"
public class Fruitlist(){...}
public class Apple() extends Fruitlist{....}

"There is a class Fruit which has a sub class orange"
public class Fruit() {...}
public class Orange() extends Fruit {...}

"FruitList uses Fruit"
import Fruit;
public class FruitlList(){...}

"Apple uses Orange"
import Orange;
public class Apple(){...}

That's what I'm making out of it?

yes,you got it correct
but here is another approach:

the person who asked this question might mean "object creation" by term "uses it"

like
public clasd FruitList
{
Fruit fr = new Fruit();
}

what is the answer in both cases?

I don't understand?
The above code is also correct. Just have to include the import statement.. Like:

import Fruit;
public class FruitList
{
    Fruit fr = new Fruit();
}

let me explain more deeply

//FRUIT CLASS

public class Fruit {
    
    public void name(){
    System.out.println("add a fruit");
    }
    public void color(){}

}


//ORANGE CLASS

public class Orange extends Fruit{
    
      public void name(){
    System.out.println("Hello Im Orange");
    }
    public void color(){
    System.out.println(" im orange in color");
    }

}

//FRUITLIST CLASS


import ams.applet.Fruit;

public class FruitList {

    public void add(){
    System.out.println("Add a new fruit");
    }
    Fruit fr = new Fruit();
    fr.color();
}

//APPLE CLASS

import Orange;
public class Apple extends FruitList {
    
    public void add(){
    System.out.println("I am apple");
    }
    
   Orange o = new ams.applet.Orange();
    o.color();

}

is anything wrong with/in the above code?the scenario is possible to implement?

I don't understand?
The above code is also correct. Just have to include the import statement.. Like:

import Fruit;
public class FruitList
{
    Fruit fr = new Fruit();
}

What if the Fruit class is given default access. It won't be accessible outside its package.

This article has been dead for over six months. Start a new discussion instead.