(3) Write a program to declare some int variables and string variables as protected and public and access them in another class?
public class Task7
{
public static void main(String[] args)
{
& Cat kitten = new Cat(1, "Murka");
& kitten.printInfo();
}
}
// The super class that implements one public variable and one protected variable
// These variables would be used in the child class
class Animal
{
public int age;
protected String name;
Animal(int pAge, String pName)
{
& age = pAge;
& name = pName;
}
public void printInfo()
{
}
}
// The derived class access the variables of the superClass
class Cat extends Animal
{
Cat(int pAge, String pName)
{
& super(pAge, pName);
}
public void printInfo()
{
& // The access to the public int variable and protected string variable
& System.out.print("Cat, it's age is " + age + " years and it's name is " + name);
}
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
JavaJSPJSF