JAVA HOMEWORK Question 1: Describing Inheritance Rules In your own words, descri
ID: 3673526 • Letter: J
Question
JAVA HOMEWORK
Question 1: Describing Inheritance Rules
In your own words, describe the following:
1.What are the rules for accessing public, private, and protected variables declared in a parent class from the child class?
2.What are the two uses of the super keyword?
3.When and how do you override a method?
Question 2: Design a Parent/Child Class
Design classes to represent an animal, a dog, and a cat. (Perhaps to be used in a veterinary hospital or pet boarding center). Sketch out the following. (You do not need to write the code.)
·What are the parent/child relationships between the classes?
·What instance data variables belong in each class (list the name and type)?
·What methods would go in each class (list the name, return type, and parameter list)?
Explanation / Answer
Question 1. Ans:-
1. Ans:-
Inheritance is a process of making use of the exciting details of a class by other class. The class which is going to extend the properties of other class is called as child class or sub class or derived class or dependent class. The class which is providing the properties o the sub class is called as parent class or super class or base class.
In java only single Inheritance is possible that means a sub class can extend only one super class at a time.
2. Ans:-
creating an instance to the child class and invoking the constructor method in the parent class is nothing but constructor chaining. the constructor in the parent class is invoked by its child class by making a statements called "Super keyword" in the child class constructor as a first statement. constructor cannot be overloaded in inheritance.
eg:- package Inheritance;
class Ex_cc
{
public Ex_cc(int a)
{
System.out.println("the value in parent class is"+ a);
}
}
class Ex_cc1
{
public Ex_cc1(int a)
{
Super(a);
System.out.println("the value in child class is"+a);
}
public static void main(String a[])
{
Ex_cc1 b=new Ex_cc1(10);
}
}
output:- value in parent class is 10
value in child class is 10.
3. Ans:-
Method ove riding is a technique of implmenting the same method signature( same method name, same return types, same no.of parameters etc..) of a parent by the child is called as method overriding. To execute the methods in both parent and child classes, we have to create individual objects of each class and we have called the methods. Overriding is done in the case that is if the parent class fails to satisfy the complete properties to its child. Then the child tries to execute same properties in its own class.
eg:-
package Inheritance;
class A
{
public void getData(int a)
{
System.Out.println("value in parent"+a);
}
}
class B extends A
{
public void getData(int a)
{
System.out.println("value in child"+a);
}
public static void main (String a[])
{
A a1=new A();
B b=new B();
a1.getData(10);
b.getData(10);
}
}
output:-
value in parent 10
value in child 10.
Question 2.Ans:-
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.