Question

In: Computer Science

If a class A implements interface I1 and class C and D are derived from class...

If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct

1. A, C, and D

2. A and D

Solutions

Expert Solution

Ans)

  • Objects refrance of Class A,C and D can be stored in the variable type of interface I1.  
  • Because refrance of interface I1 is first points to subclass,first it will check the subclass if the method is not avilable in sublass then it will find that method in its super(Parent) class
  • For better understanding i am sharing the code,you can refer thaat code for ur understanding

I1.java


public interface I1 {
   int ref=0;
   public void reef();

}
////

A.java


public class A implements I1{
   int ref=0;

   public void reef()
   {
       System.out.println("in class A");
   }
}

///

c.java


public class C extends A {
   public void reef()
   {
       System.out.println("in class C");
   }

}

//

D.java


public class D extends A {

  

}

///

Test.java


public class test {
public static void main(String[] args) {
  
   I1 d=new D();
   d.reef();
  

   I1 c=new C();
   c.reef();
   I1 a=new A();
   a.reef();
          
  
}
}
///

In the above code you can see that there is no method in class D ,hence the refrance pointer of Interface I1 will check in Class A to print method D

Screenshot of the result in console is shown below:

Thank You!


Related Solutions

Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in...
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in Chapter 7. Use an instance of the class ArrayList to contain a queues entries. Then write a driver/test program adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList. Deliverables: EmptyQueueException.java (given in Lab_7_StartUp folder) QueueInterface.java (given in Lab_7_StartUp folder) ArrayListQueue.java (need to create) Lab7.java (need to create) QueInterface: @author Frank M. Carrano @author Timothy M....
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive....
C++ Code Vehicle Class The vehicle class is the parent class of a derived class: locomotive. Their inheritance will be public inheritance so reflect that appropriately in their .h files. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +setName(s:string):void +getName():string +getMap():char** +getSize():int +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator−−():void +determineRouteStatistics()=0:void The class variables are as follows: • map: A 2D array of chars, it will represent the...
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive....
C++ Code Vehicle Class The vehicle class is the parent class of the derived class: dieselLocomotive. Their inheritance will be public inheritance so reect that appropriately in their .h les. The description of the vehicle class is given in the simple UML diagram below: vehicle -map: char** -name: string -size:int -------------------------- +vehicle() +getSize():int +setName(s:string):void +getName():string +getMap():char** +setMap(s: string):void +getMapAt(x:int, y:int):char +~vehicle() +operator--():void +determineRouteStatistics()=0:void The class variables are as follows: map: A 2D array of chars, it will represent the map...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static stack of integers. Write a template that will create a static stack of any data type. Demonstrate the class with a driver program. Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program. Static Queue Template...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT