String Inheritance class Testpublic static void mainString
Solution
Here is the code explained for you:
class CatsAndDogs
{
public static void main(String[] args)
{
String s1 = \"Campus\";
String s2 = new String(s1);
String s3 = s1;
System.out.println(s1.equals(s2)); //s1 and s2 are the same, and will print true.
System.out.println(s1.equals(s3)); //s1 and s3 are the same, and will print true.
System.out.println(s1 == s3); //s1 == s3 returns true, and will print true.
System.out.println(s1 == s2); //s1 == s2 returns false, as both are not the same objects, and will print false.
System.out.println(s2.replace(\"a\", \"XY\")); //Every character of a is replaced with XY in s2. So, the output is: CXYmpus.
Cat c = new Cat(); //Calls the constructor for Cat(), and will print Bridgewater.
Dog d = new Dog(); //Calls the constructor for Dog(), and will print Westfield.
System.out.println(d.data1); //Prints the data1 of dog object, so the output is: 5.
System.out.println(d.data2); //Prints the data2 of dog object, so the output is: 70.
System.out.println(d.data3); //Prints the data3 of dog object, so the output is: Edison.
System.out.println(c.data1); //Prints the data1 of cat object, so the output is: 60.
System.out.println(c.data2); //Prints the data2 of cat object, so the output is: 10.
System.out.println(c.data3); //Prints the data3 of cat object, so the output is: Union.
d.fun(); //Call the non-parameterized function fun() of dog object, and will print: Harrison.
d.fun(3); //Call the parameterized function fun(3) of dog object, and will print: Kearny.
c.fun(); //Call the non-parameterized function fun() of cat object, and will print: Princeton.
c.fun(3); //Call the parameterized function fun(3) of cat object, and will print: Bloomfield.
}
}
class Animal
{
int data1 = 5;
int data2 = 10;
String data3 = \"Boston\";
void fun()
{
System.out.println(\"Princeton\");
}
void fun(int x)
{
System.out.println(\"Bloomfield\");
}
}
class Mammal extends Animal
{
int data2 = 40;
String data3 = \"Edison\";
void fun()
{
System.out.println(\"Newark\");
}
void fun(int x)
{
System.out.println(\"Kearny\");
}
}
class Cat extends Animal
{
String data3 = \"Union\";
int data1 = 60;
Cat()
{
System.out.println(\"Bridgewater\");
}
}
class Dog extends Mammal
{
Dog()
{
System.out.println(\"Westfield\");
}
int data2 = 70;
void fun()
{
System.out.println(\"Harrison\");
}
}
![String & Inheritance class Test{public static void main(String[] args) {String s1 = \ String & Inheritance class Test{public static void main(String[] args) {String s1 = \](/WebImages/25/string-inheritance-class-testpublic-static-void-mainstring-1065439-1761557237-0.webp)
![String & Inheritance class Test{public static void main(String[] args) {String s1 = \ String & Inheritance class Test{public static void main(String[] args) {String s1 = \](/WebImages/25/string-inheritance-class-testpublic-static-void-mainstring-1065439-1761557237-1.webp)