Question

Consider the following Scala code that uses classes, objects, inheritance and dynamic method binding abstract class A { def m(b) Draw and explain the in-memory structures that would be used to implement the object-oriented code above, assuming that v

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

Ans a) In Scala object oriented features deal with classes and objects. I would like to explain the concept in terms of above code in a clear way. Here in above code we have abstract base class and inherited sub classes. In Scala also we have single,multilevel,multiple and Hierarchical Inheritances supported. The Subclasses can have their own methods and variables declared and used, at the same time have acces to all the methods and variables in base class. Basically Reusability is achieved. This is how inheritance works in Scala. When object is created for a particular subclass in main then immediately all the instances of variables and methods in base class is loaded into memory. Thats how the inherited subclasses can acces all the variables and methods in base class.

In above code Class A is abstract class whose variables and methods are only accesible to its inherited class. Function m() is been overrided in inherited classes B and C. If object is created for either of the classes B and C and if function m() is called using created object in main then you would get the overriden function m() of that particular subclass for which the object is created. Here the abstract class A is instantiated which is violating the rule of scala which states that "Abstract classes cannot be instantiated in scala". So in above code the object is created for class A which gives you error. So with obj.m() no method is called.

Ans b)Abstract class A mc) Istall a-2 Base class Inheritance & class B b= öne mer: Int=3 h(): Ist = 4 class c x=5 m(): Int=6 Inher

when object is created for any of inherited classes. The instances of all variables and methods of Base class o are loaded inthe Here m() in at m() base noen overides class when time method called If object is created for class B, then methods calledIn above code if A is instantiated val obj = new AC) we get an error steting closs A is abstract cannot be Instantiated

Add a comment
Know the answer?
Add Answer to:
Consider the following Scala code that uses classes, objects, inheritance and dynamic method binding abstract class...
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
  • 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...

  • Consider the following object-oriented code: abstract public class Number ( abstract public Number add(Number n); public...

    Consider the following object-oriented code: abstract public class Number ( abstract public Number add(Number n); public class Complex extends Number [ public Complex(double r, double i) public Complex add(Number n) [ Complex that - (Complex) n; return new Complex(this.re+that.re, this.im+that.im); private double re; private double im; public class Real extends Number public Real(double v) val-v public Real add(Number n) Real that Rea) n; return new Real(this.val+that.val) private double val; Unsurprisingly, the following code compiles. Number n- new Complex(2,3).add(new Real(1)); What...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • 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...

  • All question base on Java Se 8 Consider the Top class i public abstract class Topí...

    All question base on Java Se 8 Consider the Top class i public abstract class Topí 2 private 3 protected String name; 4 public intx - 12; int age; e public Top(String name)[ this.namename age0; System.out.println(x); 10 12 public abstract void foo(String f); 13 Create a Middle class that extends the Top class. The class should have a single constructor that takes two input parameters: a String (for name) and int (for age). Your constructor should not have any redundant...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

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