Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package com.mac 1 9 0 . inheritance; / * Inheritance is used whenever an object of a class can be considered as also an object
package com.macinheritance;
Inheritance is used whenever an object of a class can be considered as also an object
of another class. Example: I have a class Animal, you want to design a class Dog, A Dog IS
an animal, then the class Dog should be designed using inheritance from class Animal.
When inheritance is used a class inherits all member variables and methods of the inherited class.
Still private members of the parent class cannot be accessed directly by the inheritedchild class.
If an inherited class needs to access member variables of a parent class, then you can use protected.
protected can be accessed by inherited class but not the public.
Let's have an example:
create a class animal that has the following:
color, age, species, diet
toString, equals
method speak to display I don;t know how to speak yet!.
public class Animal
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started