In regards to fields and methods, what happens when one class inherits from anot
ID: 3561376 • Letter: I
Question
In regards to fields and methods, what happens when one class inherits from another? If a reference variable is polymorphic, what types of objects can it refer to? What does the super reference refer to? If a class overrides a method that it inherited, and an object is created from that child class, which version of the method is called when that object calls it (child or parent)? How does the "is-a" relationship relate to inheritance? public class Freshman extends Student In the code above, which is the parent class? Place the following classes in a logical hierarchy. Draw boxes with the class name to represent the class, and lines connecting the classes to represent inheritance.Explanation / Answer
Part -1 )
1) if the fields and methods are PRIVATE then those are not available in child class,,,,other than PRIVATE i.e PUBLIC,PROTECTED and DEFAULT then all this fields and methods are available in child class.
2) by using polymrphic reference variable we can refer both parent and child class Objects
Ex :
class Shape
{
}
class Circle extends Shape
{
}
class Line extends Shape
{
}
in main()
Shape obj = new Shape(); // valid
obj = new Line(); // valid
obj = new Circle(); // valid
Circle obj2 = new Shape(); // not valid
Line obj2 = new Shape(); // not valid
3) by using the "super" we can call to "super class Constructor".
Ex :
class Person
{
String gender;
Person(String gender)
{
this.gender = gender;
}
}
class Student extends Person
{
int id;
Student(String gender,int id)
{
super(gender);
this.id = id;
}
}
in above from Student class Constructor we are calling to parent( super ) class Constructor.
Note: 'super' is the FIRST statement in Constructor.
4) if we OVERRIDE the parent class Method in Child class, and if we create Object for child class then Child VERSION method we be Executed.
5) IS-A is a way of saying : This object is a type of that object. Let us see how the 'extends' keyword is used to achieve "inheritance".
class Person
{
}
class Employee extends Person
{
}
Employee "is-a" Person
class Student extends Person
{
}
Student "is-a" Person
Part-2)
1) Student is the Parent class
2) Animal
^ ^ ^
|
|
|
Dog
^ Cat
|
|
|
|
Husky( Puppy )
Bird
^
|
|
|
|
Cardinal
3) class Animal
{
}
class Dog extends Animal
{
}
class Cat extends Animal
{
}
class Huskey extends Dog
{
}
class Bird extends Animal
{
}
class Cardinal extends Bird
{
}
4) it will refer the super class i.e Animal class
5) // parent class reference
Dog obj = new Dog();
// child class reference
Dog obj = new Huskey();
6) public class Animal
{
private String typeOfMovement;
private double moveSpeed;
public Animal(String typeOfMovement,double moveSpeed )
{
this.typeOfMovement = typeOfMovement;
this.moveSpeed = moveSpeed ;
}
}
public class Bird extends Animal
{
private boolean canFly;
private int wingSpan;
public Bird(String typeOfMovement,double moveSpeed, boolean canFly, int wingSpan)
{
super(typeOfMovement, moveSpeed );
this.canFly = canFly;
this.wingSpan = wingSpan;
}
}
public class Cardinal extends Bird
{
private String shadeOfRed;
public Cardinal(String typeOfMovement,double moveSpeed, boolean canFly, int wingSpan, String shadeOfRed)
{
super(typeOfMovement, moveSpeed, canFly, wingSpan);
this.shadeOfRed= shadeOfRed;
}
}
7) if we have private fields in Super class then those are not DIRECTLY accesible from child class,, so we create public METHODS ( setter's & getter's ) of that Private variables and call that Methods.
@Override
public String toString()
{
return getMoveType()+" "+getMoveSpeed()+" "+pureBlood+" "+furColor+" "+age;
}
Note: getMoveType() and getMoveSpeed() are getter Methods of Animal class private fields i.e moveType and moveSpeed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.