Question

Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and...

Abstract classes and Interfaces problems

10. Explain one similarity and one difference between abstract classes and interfaces.

11. Consider the following declarations.

public interface Shape{

int someFunction(Shape other);

//other functions not shown

}

public class Square implements Shape

{/*implementation not shown*/}

Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface?

  1. public int someFunction (Shape other)
  2. public int someFunction (Square other)
  3. public boolean someFunction(Object other)

12. In Java, all functions implement late binding, except when they are labeled with two keywords: which ones?

13. What is the output of the following code?

interface Vessel { }

interface Toy { }

class Boat implements Vessel { }

class Speedboat extends Boat implements Toy { }

public class Test {

     public static void main(String[] args) {

          String s = "0";

          Boat b = new Boat();

          Boat b2 = new Speedboat();

          Speedboat s2 = new Speedboat();

if((b instanceof Vessel) && (b2 instanceof Toy))

s += "1";

          if((s2 instanceof Vessel) && (s2 instanceof Toy))

s += "2";

          System.out.println(s);

     }

}

14. True/false

_____ When implementing an interface, the compiler will ensure that the method implementation has the intended behavior.

_____ An interface can be extended through inheritance.

_____ Variables can be added to an interface only if they are static, public and final.

_____ An interface should have a constructor.

_____ An abstract class can not implement an interface.   

_____ The Serializable interface includes no methods and no defined constants

15. What is the output of the following code?

public abstract class Base {

     public Base()

     { System.out.println(“Building Base class”); };

     public void message()

     { System.out.println(“Greetings from Base class”); };

}

public class Derived extends Base {

     public Derived()

     { System.out.println(“Building Derived class”);   };

     public void message()

     { System.out.println(“Greetings from Derived

class”); };

}

public class Test{

     public static void main(String[] args) {

          Base b = new Base();

          Base d = new Derived();

          b.message();

          d.message();

     }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

10)

Difference

Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior

Similarity

Interface can not be instantiated. Same way, you can not instantiate abstract class.

That means you can not create object of interface or abstract class.

11)

public int someFunction (Shape other)

because this function is declared in the interface called shape and Square class is implements that interface so Square class have to implements this method.

12)

abstract and interface

13)

012

because Boat class implements vessel and Speed class extends from the Boat and implements the Toy so all the condition is true.

14)

1)True

2)True

A class implements an interface, thereby inheriting the abstract methods of the interface.

3)True

Any implementations can change value of fields if they are not defined as final. Then they would become a part of the implementation.An interface is a pure specification without any implementation.

If they are static, then they belong to the interface, and not the object, nor the run-time type of the object.

An interface provide a way for the client to interact with the object. If variables were not public, the clients would not have

access to them.

4)False

The answer is No, interface cannot have constructors. ... In order to call any method we need an object since there is no need to have object of interface, there is no need of having constructor in interface .

5)false

abstract class implements the interface

6) True

15)

Ouput

Building Base class

Building Derived class

Greetings from Base class

Greetings from Derived class

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between ab...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • In each of the following questions, first use NetBeans IDE to run the code (if there...

    In each of the following questions, first use NetBeans IDE to run the code (if there is an error fix it) then take a screenshot of the output and paste it under (A). In (B), justify the output you obtained in (A). - 9. Given the following: public class WorkingSheet {     public static void main(String[] args) {        B myB = new B();        A myA = new B();        System.out.print(myB instanceof A);        System.out.print(myB instanceof C);        System.out.print(myA...

  • Show the output of running the class Test in the following code lines: interface A {...

    Show the output of running the class Test in the following code lines: interface A { void print (); } class C ( class B extends C implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); w class Test { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • Show the output of running the class Test in the following code lines: a) Nothing. b)...

    Show the output of running the class Test in the following code lines: a) Nothing. b) b is an instance of A followed by b is an instance of c c) b is an instance of C d) b is an instance of A interface A { void print (); } class C {} class B extends c implements A { public void print() { } } class Test { public static void main(String[] args) { B b = new...

  • D Question9 Which of the following represents the output of the following code segment? abstract class...

    D Question9 Which of the following represents the output of the following code segment? abstract class Base Base0System.out.println("Base Constructor Called"); abstract void fun: class Derived extends Base Constructor Called"): fun) called"): ) class MainClass Derived0 System.out.println("Derived void fun [System.out.printin/"Derived public static void main(String args) Derived d = new Derived@; O Print "Derived Constructor Called then print Base Constructor Called O Print "Base Constructor Called then print Derived Constructor Called" O We must write @override above the fun) method in Derived...

  • 1. (TCO 7) Which of the following statements are true? (Points : 5)        Interfaces contain...

    1. (TCO 7) Which of the following statements are true? (Points : 5)        Interfaces contain one and only one implemented method, a constructor.        Interfaces are defined inside an abstract class.        All methods defined in an interface must be implemented when used by another class.        A true object-oriented design must contain as least one interface. Question 2. 2. (TCO 7) In an object-oriented program, methods with no implementation might be found in an _____ and/or a(n) ______....

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT