Question

As an object oriented language, Java obviously provides support for composition and inheritance. Let's start off...

As an object oriented language, Java obviously provides support for composition and inheritance. Let's start off by describing how Java specifies that one class inherits from another class. Are there any restrictions on inheritance in Java that distinguish it from C++? How does a derived class override a base class method? How does an overridden method invoke a base class method? How does the derived class constructor invoke an explicit value constructor from the base class?

Note: Plagiarism is checked

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

Inheritance is one of the important feature in OOP. Inheritance says that the child can inherit the features of Parent, here child acts as derived class and parent acts as base class.

Lets see how Inheritance works in Java.

In java, the child class or the derived class inherit the parent or base class features by using "extends" keyword.

For Example:

Class Child extends Parent
{


}

Here class Child inherits the features of Parent.

Are there any restrictions in inheritance in Java that distinguish C++?


In Java multiple inheritance is not allowed through classes which means that derived class cannot inherit features of more than one base class.

By default every class is inherited from the Object class in Java and whereas in C++ it doesn't inherit anything.

This can be done through interfaces in Java. Derived Interfaces use implements keyword to inherit the features from the base interfaces

For Example:

Interface Abc implements Def, Ghi


How does a derived class override a base class in java?

Consider a base class Shape and a derived class Rectangle. Both have a method called draw().

Class Shape
{

public void draw()
{

}


}

Class Rectangle extends Shape
{


public void draw()
{

}

public static void main(String[] args)
{
Shape s= new Rectangle(); // Here we are creating a derived class object and asssigning it to base class reference. JVM will decide which objectto call during runtime.
s.draw(); // The reference is base class but draw method gets overrided during runtime as derived class object is assigned to base class


}

}

How does a overriden method invoke a base class method?

In derived class method call base class method by using Super keyword.

draw() // method in Rectangle class
{

super.draw(); // This will invoke base class method
}

How does the derived class constructor invoke an explicit value constructor from the base class in java?


// Shape class Constructor

Shape(int area)
{
this.area=area;
}

To invoke this base class constructor in the derived class constructor, use super keyword in Rectangle class

// Rectangle class constructor

Rectangle(int area)
{

super(area);

}


Please upvote if you like the answer and please comment if you need any further information in this regard.

Add a comment
Know the answer?
Add Answer to:
As an object oriented language, Java obviously provides support for composition and inheritance. Let's start off...
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
  • JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance...

    JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance and polymorphism. In this assignment we will use inheritance and polymorphism to solve a problem. Part (a) of the figure below shows a symbolic representation of an electric circuit called an amplifier. The input to the amplifier is the voltage vi and the output is the voltage vo. The output of an amplifier is proportional to the input. The constant of proportionality is called...

  • How do I start this Java: Inheritance problem? • person.java • student.java • studentAthlete.java • app.java...

    How do I start this Java: Inheritance problem? • person.java • student.java • studentAthlete.java • app.java • Students should apply consistent indenting in all submissions. This can be done via the NetBeans Source menu. Contents person First name Last name app Creates 1 student-athlete object Hometown Retinfo) 1-Displays the complete information about the student-athlete object student New attributes Maior attributes getinfo) method from the superlas person Student-athlete student's rankine Overrides the method from the superclass student Video from Professor Fisher...

  • Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance...

    Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance structure of various shapes. Details: Implement the following Shape hierarchy: a box around each shape name and arrows pointing to the shape from each (Circle, Square, Triangle) Shape Circle Square Triangle    Use the following as a guide: The Shape class should be abstract and contain two abstract methods: getArea – Which calculates the area of the shape and returns that value as a...

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

  • (TCO 1) The JVM executes a Java application by (Points : 6)        executing the public static...

    (TCO 1) The JVM executes a Java application by (Points : 6)        executing the public static main method of the class.        creating an object of the class and executing that object’s main method.        executing the class constructor.        compiling the Java code into byte code. (TCO 1) Which method call converts the value in variable stringVariable to a double? (Points : 6)        Double.parseDouble( stringVariable );        Convert.toDouble( stringVariable );        Convert.parseDouble( stringVariable );        Float.parseFloat( stringVariable ); (TCO 1) Which of the following can...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • (1)     ____ is the principle that allows you to apply your knowledge of a general category...

    (1)     ____ is the principle that allows you to apply your knowledge of a general category to more specific objects.           a.       Inheritance              c.       Encapsulation           b.       Polymorphism                    d.       Override (2)     When you create a class by making it inherit from another class, you are provided with data fields and ____ automatically.           a.       fonts                      c.       class names           b.       methods                  d.       arrays (3)     By convention, a class diagram contains the ____ following each attribute or...

  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

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