8.Create a Java program to create a package with public class and protected members to be accessed in another class.
💡Code:
package pack1;
public class ParentClass
{
protected String name;
protected int age;
public ParentClass(String name, int age)
{
this.name = name;
this.age = age;
}
}
class ChildClass extends ParentClass
{
public ChildClass(String name, int age)
{
super(name, age);
}
public void accessProtectedInfo()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
class Main {
public static void main(String[] args) {
ChildClass childClass = new ChildClass("John Doe", 30);
childClass.accessProtectedInfo(); }
}
📸Output :