0% found this document useful (0 votes)
11 views3 pages

New 1sde34

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

New 1sde34

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

It looks like you shared a Java code snippet with a switch-case statement.

The code
has a string phone set to "APPLE", and the switch statement checks against
different cases. Here's a breakdown of what happens when the code runs:

java
class Example {
public static void main(String args[]) {
String phone = "APPLE";
switch(phone) {
case "Apple":
System.out.println("Apple ");
case "APPLE":
System.out.println("APPLE ");
default:
System.out.println("SAMSUNG ");
}
}
}
When the switch statement evaluates phone, it finds the case "APPLE" matches and
prints "APPLE ". Since there's no break statement after this case, the execution
continues to the default case, which prints "SAMSUNG ". Therefore, the output will
be:

APPLE
SAMSUNG
If you were to add break statements after each case, it would stop executing after
printing "APPLE ". Let me know if you'd like any further explanation or if there's
another code snippet you'd like to discuss!

In this code snippet, you have a base class Employee and a derived class
PermanentEmployee. The main method creates an object of PermanentEmployee but
refers to it using a reference of type Employee. Here’s a step-by-step explanation
of what happens:

java
class Employee {
int salary = 0;

Employee() {
salary = 10;
}

public void changeSalary() {


salary = salary + 1;
System.out.println(salary);
}
}

class PermanentEmployee extends Employee {


public void changeSalary() {
salary = salary + 2;
System.out.println(salary);
}
}

public class Test {


public static void main(String[] args) {
Employee employee = new PermanentEmployee();
employee.changeSalary();
}
}
Object Creation: Employee employee = new PermanentEmployee();

Here, an object of PermanentEmployee is created and assigned to a reference of type


Employee.

Method Call: employee.changeSalary();

Although the reference type is Employee, the actual object is of type


PermanentEmployee. Therefore, the overridden changeSalary() method in
PermanentEmployee is called, not the one in Employee.

Output:
The output of the program will be:

12
Here’s why: When the PermanentEmployee constructor is called, the Employee
constructor is also invoked, which sets salary to 10. The changeSalary() method of
PermanentEmployee then increments the salary by 2, resulting in salary being 12.

Feel free to ask if you have more questions or need further clarification!

Thanks for sharing this Java code snippet. Let's dive into it and understand what's
happening here:

java
class Base {
public int a = 3;
public void addFive() {
a += 5;
System.out.print("Base ");
}
}

class Derived extends Base {


public int a = 8;
public void addFive() {
this.a += 5;
System.out.print("Derived ");
}
}

public class Test {


public static void main(String[] args) {
Base f = new Derived();
f.addFive();
System.out.println(f.a);
}
}
Breakdown of the code:
Class Base:

Contains a public integer a initialized to 3.

Method addFive() increases a by 5 and prints "Base ".


Class Derived:

Extends Base and has its own public integer a initialized to 8.

Overrides the addFive() method to increase this.a by 5 and prints "Derived ".

Class Test:

In the main method, an object of Derived is created but referenced by a Base type
reference f.

f.addFive() calls the overridden addFive() method in Derived because of


polymorphism.

Since this.a refers to the field in Derived, it updates the a in Derived from 8 to
13 and prints "Derived ".

f.a accesses the a field in Base, which remains 3.

Output of the code:


Derived 3
So, when you run this code, it prints "Derived " and then "3". The method addFive()
in Derived modifies the a in Derived class, but the a in Base remains unaffected
when printed.

Does this explanation help you understand the behavior of the code? Feel free to
ask if you have more questions or need further clarification!

Message Copilot

You might also like