(3) Write a program to declare some int variables and string variables as protected and public and access them in another class?
1
Expert's answer
2011-08-07T07:16:58-0400
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); } }
Comments
Leave a comment