Question

47. What is overriding? Describe using java code showing an example of overriding. 48. Does Java...

47. What is overriding? Describe using java code showing an example of overriding.

48. Does Java allow us to refine behavior in subclasses? If so, show Java code that demonstrates
this property.

49. Does Java allow us to replace behavior in subclasses? If so, show Java code that demonstrates
this property.

50. Can dynamic binding be used in C++? If so, show how this is accomplished.

51. Is there a cost to providing the dynamic binding capability? If so, describe the cost with an example.

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

47- Object-oriented programming language,provides a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes called as OVERRIDING. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

// A Simple Java program to demonstrate

// method overriding in java

  

// Base Class

class Parent

{

    void show() { System.out.println("Parent's show()"); }

}

  

// Inherited class

class Child extends Parent

{

    // This method overrides show() of Parent

    @Override

    void show() { System.out.println("Child's show()"); }

}

  

// Driver class

class Main

{

    public static void main(String[] args)

    {

        // If a Parent type reference refers

        // to a Parent object, then Parent's

        // show is called

        Parent obj1 = new Parent();

        obj1.show();

  

        // If a Parent type reference refers

        // to a Child object Child's show()

        // is called. This is called RUN TIME

        // POLYMORPHISM.

        Parent obj2 = new Child();

        obj2.show();

    }

}

OUTPUT

Parent's show()
Child's show()

48-Normally, subclassing refines a class by adding variables and methods (you cannot remove or hide variables or methods by subclassing). For example

    class Animal {
        float weight;
        ...
        void eat() {
            ...
        }
        ...
    }

    class Mammal extends Animal {
        // inherits weight
        int heartRate;
        ...

        // inherits eat()
        void breathe() {
            ...
        }
    }
class Cat extends Mammal {
        // inherits weight and heartRate
        boolean longHair;
        ...

        // inherits eat() and breathe()
        void purr() {
            ...
        }
    }

49-In Java, all non-static methods are "virtual", meaning that they are based on the runtime type of the underlying object rather than the type of the reference that points to that object. Therefore, it doesn't matter which type you use in the declaration of the object, the behaviour will be the same.

50​​​​- In C++ we have the facility to specify that the compiler should match function calls with the correct definition at the run time; this is called dynamic binding or late binding or run-time binding. Dynamic binding is achieved by virtual functions. Base class pointer points to derived class object. And a function is declared virtual in base class, then the matching function is identified at run-time using virtual table entry.

using namespace std;
class A
{
public:
virtual void display()
{
cout<<"Base";
}
};

class B:public A
{
public:
void display()
{
cout<<"Derived";
}
};
int main()
{
B b;
A *a=&b;
a->display();
return 0;
}

51-The cost is negligible. It doesn't matter how many classes you have, or how many levels of inheritance, the cost of a polymorphic call is a simple addition.

Add a comment
Know the answer?
Add Answer to:
47. What is overriding? Describe using java code showing an example of overriding. 48. Does Java...
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
  • 42. When does an object become subject to garbage collection? Describe using a Java example (show code). 43. What is the main difficulty in users performing their own free storage management? 44. What...

    42. When does an object become subject to garbage collection? Describe using a Java example (show code). 43. What is the main difficulty in users performing their own free storage management? 44. What language allows users to perform their own free storage management? 45. What is overloading? Describe using java code showing an example of overloading. 46. Why would we desire to overload functions? Describe using a Java example (show code).

  • Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we ...

    Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is closest to the end (or the front) of a list, etc. We can imagine writing a simple class which will do this: a class which will let us enter new results one by one, and at any point will...

  • Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply...

    Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply inheritance and method overriding in order to create a hierarchy of array sorters. There exist many algorithms to sort arrays. In this problem set, we are especially interested in “in-place” sorting algorithms like Selection Sort and Insertion Sort. In-place sorting refers to an approach in which we perform the sorting steps on the underlying array rather than using extra storage. When using object-oriented design,...

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • Please use Java to write the program The purpose of this lab is to practice using...

    Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

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