Showing posts with label Java Object. Show all posts
Showing posts with label Java Object. Show all posts

Friday, September 15, 2023

Quiz yourself: Deserializing objects with readObject

Quiz Yourself, Oracle Java Career, Oracle Java Skill, Oracle Java Jobs, Oracle Java Prep, Oracle Java Preparation, Oracle Java Object

You should know how the readObject method works—and when it won’t compile.

Given the following Person record

record Person(String name) implements Serializable {}

and the following method fragment

try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename))) {
    ... // code here
    System.out.println(person.name());
}

Assume that the try block is completed with necessary catch blocks and the file contains a serialized Person and opens correctly.

The code from which option, when placed where the comment is, properly deserializes the object and allows the rest of the code to work? Choose one.

A. var person = (Person) null;
if (in.readObject() instanceof Person p) {
  person = p;
}
B. var person = in.readObject();

C. Person person = null;
if (in.readObject() instanceof Person) {
  person = in.readObject();
}

D. var person = null;
Object o = in.readObject();
if (o instanceof Person) {
  person = (Person) o;
}

Answer. We will skip over option A for the moment.

Option B would read the serialized object from the file, but the return type of the readObject method is declared as Object; therefore, var will infer the type of the person variable to be Object too. This means that person.name() will not compile. From this you can see that option B is incorrect.

Option C is incorrect for two reasons. The semantics of the code would cause the input stream to be read twice, and the result of the first reading operation would be lost. However, there is a more severe problem; as already mentioned, the return type of readObject is Object, and the following assignment to the person variable (which is declared explicitly as being of type Person in this option) would fail unless an explicit cast were added:

person = in.readObject(); // assignment fails!

The following cast would fix the problem:

person = (Person)in.readObject(); // this could work

Option D is also incorrect. When var is used to declare a local variable (other than a lambda formal parameter), the compiler must be able to decide, unambiguously, the intended type of the variable based on the expression that is assigned for initialization. The null value does not provide type information—it is assignment-compatible with any reference type. So, compilation would fail.

Changing the declaration to the following would allow option D to work:

var person = (Person) null;

Option A is correct. It uses the newer “pattern matching for instanceof” feature that was added (after several previews) in Java 16 as JEP 394. The effect is that if the deserialized object is, in fact, assignment-compatible with the Person type, the variable p is declared and initialized with the reference to that object. The scope of p is such that it’s usable only in the parts of the code where it has definitely been initialized. For example, if there were an else clause on the if statement, p would be out of scope in that else clause.

Conclusion. The correct answer is option A.

Source: oracle.com

Wednesday, May 24, 2023

Quiz yourself: When is a Java object still reachable?

Quiz yourself, Java object, Oracle Java, Oracle Java Certification, Oracle Java Prep, Java Career, Java Guides, Java Learning, Java Object

The garbage collector can’t collect an object until it’s no longer available to any live thread.


Given the following code

interface Repairable {}
class Car implements Repairable {
  class Clutch implements Repairable {}
  private Clutch c;
  public Car() {
    c = new Clutch();
  }
  public Clutch getClutch() {
    return c;
  }
}

and a method fragment

01: var rl = new ArrayList<Repairable>();
02: var car = new Car();
03: var clutch = car.getClutch();
04: var engine = (Repairable) null;
05: rl.add(car);
06: rl.add(clutch);
07: car = null;
08: clutch = null;
09: rl.add(engine);
10: rl.set(2, engine);
11: rl.remove(0);
12: rl.remove(1);

After which line will the Car instance created at line 02 become eligible for garbage collection? Choose one.

A. It will become eligible after line 07.
B. It will become eligible after line 08.
C. It will become eligible after line 10.
D. It will become eligible after line 11.
E. It will become eligible after line 12.
F. It will not become eligible after line 12.

Answer. An object becomes eligible for the garbage collection process when it is no longer reachable. Section 12.6.1 of the Java Language Specification states the following about finalization:

A reachable object is any object that can be accessed in any potential continuing computation from any live thread.

For this question, it’s sufficient to consider only the single thread executing this code and the variables that are declared within it. There are four such variables: rl, car, clutch, and engine. From those variables, you should consider the transitive reachability. That is, if you have added car to the list, you can get at car through the reference rl, because you can follow rl to the list and then from the list you can access car. All such transitive reachability must be considered.

Now, look at the state of these variables at key points in the code.

By the end of line 06, the variable rl refers to the ArrayList, and that list contains references to both the Car and the Clutch objects. At that same point, the car variable also refers to the Car object. Clearly the Car object is reachable at this point.

By the end of line 10, the car and clutch variables have been nulled out—but the list still contains references to both the Car and Clutch objects, so the Car object is still reachable.

At this point, the list’s contents are the car, the clutch, and null. Note that line 09 appends engine (which contains null) to the list, placing null at index 2. Then line 10 sets the value at index 2 to the engine value: null. Line 10, therefore, has no meaningful effect on the state of things.

Line 11 removes the item at index 0, which is the car. At first sight, that might seem to be the last reference to the car, but you’ll see in a moment that it’s not. Continuing to trace the execution, at this point the list now contains the clutch and null.

Line 12 removes the item at index 1, which is the null. The list, therefore, contains nothing except the clutch.

What’s not immediately obvious is that the Clutch object itself, which is an inner class, contains a reference to its enclosing instance, which is the Car object. Because of that reference, the Car object is still reachable after line 12, which makes option F the correct answer and the other options incorrect.

This reference to the enclosing instance is usable by the programmer. If you were to add a toString method, such as the following, to the Clutch the reference, Car.this would provide access to the car to which this clutch belongs. It’s worth noting that in Java, the access control rules are such that the features of a Car object are accessible from the Clutch class even if they are private, and the features of Clutch are likewise accessible to Car—again, even if they are private.

class Clutch implements Repairable {
  @Override
  public String toString() {
    return "Clutch of car " + Car.this;
  }
}

Conclusion. The correct answer is option F.

Source: oracle.com

Friday, March 19, 2021

Java Classes and Objects

Core Java, Oracle Java Certification, Oracle Java Exam Prep, Oracle Java Preparation, Oracle Java Learning

Java Classes/Objects

Java is an object-oriented programming language.

Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class:

Main.java

Create a class named "Main" with a variable x:

public class Main {

  int x = 5;

}

Create an Object

In Java, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects.

To create an object of MyClass, specify the class name, followed by the object name, and use the keyword new:

Example

Create an object called "myObj" and print the value of x:

public class Main {

  int x = 5;

  public static void main(String[] args) {

    Main myObj = new Main();

    System.out.println(myObj.x);

  }

}

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of Main:

public class Main {

  int x = 5;

  public static void main(String[] args) {

    Main myObj1 = new Main();  // Object 1

    Main myObj2 = new Main();  // Object 2

    System.out.println(myObj1.x);

    System.out.println(myObj2.x);

  }

}

Using Multiple Classes

Core Java, Oracle Java Certification, Oracle Java Exam Prep, Oracle Java Preparation, Oracle Java Learning
You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)).

Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory/folder:

◉ Main.java

◉ Second.java

Main.java

public class Main {

  int x = 5;

}

Second.java

class Second {

  public static void main(String[] args) {

    Main myObj = new Main();

    System.out.println(myObj.x);

  }

}

When both files have been compiled:

C:\Users\Your Name>javac Main.java

C:\Users\Your Name>javac Second.java

Run the Second.java file:

C:\Users\Your Name>java Second

And the output will be:

5

Friday, October 2, 2020

Difference between Inheritance and Polymorphism in Java and Object Oriented Programming

Both Inheritance and Polymorphism are key OOP concepts and similar to Abstraction and Encapsulation, they are also closely related to each other. Because of their similarities, many OOP programmers, especially beginners get confused between Inheritance and Polymorphism. Even though they are closely related and you need Inheritance to support runtime Polymorphism they are a totally different concept. Inheritance refers to the ability for classes or objects to inherit properties of other classes or interfaces. It means you can write code for common functionalities and reuse it at different places by just using Inheritance and not re-writing those codes again and again. For example, you can write code to

Inheritance vs Polymorphism in Java and Object-Oriented Programming

Let's revisit some key differences between Inheritance and Polymorphism in object-oriented programming

1) Class vs Object

Inheritance is used to define a class or interface hierarchy. You extract common functionality on superclass and allow derived classes to get more specific by adding specific functionality. On the other hand, Polymorphism allows you to do the same operation differently depending upon which context and which object is doing the operation.

2)  Code Reuse

One of the key advantages of Inheritance is code reuse. You don't need to write the same code again and again if it is needed by multiple classes. You can extract the common functionality on the base class and let other classes simply use inheritance to get that functionality. In another word, it reduces the amount of duplicate code and promotes DRY practice.

For example, if you are designing a class hierarchy for Finance and Insurance industry, you can create a base class called Insurance, which should have basic properties like covered, the sum assured, premium, etc.

Now, if your application needs to support automobile insurance like the CarInsurance, it just needs to extend the Insurance base class to add specific details required by car insurance companies like the registration number of the car, brand, etc.

Similarly, Health Insurance applications can reuse the same base class for calculating premiums, keeping a record of sum assured, and other basic details. They can further enrich the derived class by adding more specific details required by health insurance companies like pre-existing diseases, co-payment details, etc.

Oracle Java Exam Prep, Oracle Java Learning, Oracle Java Tutorial and Material, Java Prep, Java Certification

Polymorphism can also come handy here to write code for calculating premiums. Assuming, the premium is calculated differently for different types of insurance then you can override the calculatePremium() method in derived or subclasses to represent different types of premium calculation.

The benefit of using Polymorphism here is that all the common functionality like report generations, sending premium receipts, or premium reminders can still use the same the calculatePreimum() method for performing their duty. They don't need to worry about the fact that different types of insurance are calculating premium using different formulas.

3)  Flexibility

If Inheritance provides "code re-usability" then Polymorphism provides the "flexibility" to your code. As I mentioned in my previous example, you can add a new kind of Insurance without re-writing code to generate receipts, premium reminders, and other kinds of reports.
By using Polymorphism, you can encapsulate the code which is different inside the same method declared by the superclass. The Clean Code by Uncle Bob has a good write up and example on this.

Oracle Java Exam Prep, Oracle Java Learning, Oracle Java Tutorial and Material, Java Prep, Java Certification

4)  Right Usage

One of the best practices in the OOP world is using Inheritance for defining type hierarchies using the interface. This is also advised in Effective Java 2nd Edition by Joshua Bloch. By defining types, you create a path for Polymorphism.

5) extends vs implements

Inheritance in Java is implemented using "extends" and "implements" keyword. A class can extend another class or implement one or more interfaces. When it does that, the parent-child relationship is established between two classes or interfaces.

For example, in the following code, the MyClass is now a child or both Canvas and Runnable:

public class MyClass extends Canvas implements Runnable{

@Overide
public void paint(Graphics g){
   ... some code
  }

@Override
public void run(){
  ... some code
  }

}

This means you a reference variable of Runnable or Canvas can point to an object of MyClass and if you call paint() or run() method then these methods will be called instead of paint() method defined in Canvas and run() method defined in Runnable. This is the power of Polymorphism which comes from Inheritance.

Oracle Java Exam Prep, Oracle Java Learning, Oracle Java Tutorial and Material, Java Prep, Java Certification

Source: javarevisited.blogspot.com

Saturday, June 20, 2020

Java - Object and Classes

Java Object and Classes, Oracle Java Study Materials, Oracle Java Exam Prep

Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts −

◉ Polymorphism
◉ Inheritance
◉ Encapsulation
◉ Abstraction
◉ Classes
◉ Objects
◉ Instance
◉ Method
◉ Message Passing

In this chapter, we will look into the concepts - Classes and Objects.

◉ Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

◉ Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

Objects in Java


Let us now look deep into what are objects. If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior.

If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail, running.

If you compare the software object with a real-world object, they have very similar characteristics.

Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.

So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

Classes in Java


A class is a blueprint from which individual objects are created.

Following is a sample of a class.

Example

public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

A class can contain any of the following variable types.

◉ Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

◉ Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

◉ Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.

A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.

Following are some of the important topics that need to be discussed when looking into classes of the Java Language.

Constructors


Java Object and Classes, Oracle Java Study Materials, Oracle Java Exam Prep
When discussing about classes, one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class.

Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.

Following is an example of a constructor −

Example

public class Puppy {
   public Puppy() {
   }

   public Puppy(String name) {
      // This constructor has one parameter, name.
   }
}

Java also supports Singleton Classes where you would be able to create only one instance of a class.

Note − We have two different types of constructors. We are going to discuss constructors in detail in the subsequent chapters.

Creating an Object


As mentioned previously, a class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects.

There are three steps when creating an object from a class −

◉ Declaration − A variable declaration with a variable name with an object type.

◉ Instantiation − The 'new' keyword is used to create the object.

◉ Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Following is an example of creating an object −

Example

public class Puppy {
   public Puppy(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myPuppy
      Puppy myPuppy = new Puppy( "tommy" );
   }
}

If we compile and run the above program, then it will produce the following result −

Output

Passed Name is :tommy

Accessing Instance Variables and Methods


Instance variables and methods are accessed via created objects. To access an instance variable, following is the fully qualified path −

/* First create an object */
ObjectReference = new Constructor();

/* Now call a variable as follows */
ObjectReference.variableName;

/* Now you can call a class method as follows */
ObjectReference.MethodName();

Example

This example explains how to access instance variables and methods of a class.

public class Puppy {
   int puppyAge;

   public Puppy(String name) {
      // This constructor has one parameter, name.
      System.out.println("Name chosen is :" + name );
   }

   public void setAge( int age ) {
      puppyAge = age;
   }

   public int getAge( ) {
      System.out.println("Puppy's age is :" + puppyAge );
      return puppyAge;
   }

   public static void main(String []args) {
      /* Object creation */
      Puppy myPuppy = new Puppy( "tommy" );

      /* Call class method to set puppy's age */
      myPuppy.setAge( 2 );

      /* Call another class method to get puppy's age */
      myPuppy.getAge( );

      /* You can access instance variable as follows as well */
      System.out.println("Variable Value :" + myPuppy.puppyAge );
   }
}

If we compile and run the above program, then it will produce the following result −

Output

Name chosen is :tommy
Puppy's age is :2
Variable Value :2


Source File Declaration Rules



As the last part of this section, let's now look into the source file declaration rules. These rules are essential when declaring classes, import statements and package statements in a source file.

◉ There can be only one public class per source file.

◉ A source file can have multiple non-public classes.

◉ The public class name should be the name of the source file as well which should be appended by .java at the end. For example: the class name is public class Employee{} then the source file should be as Employee.java.

◉ If the class is defined inside a package, then the package statement should be the first statement in the source file.

◉ If import statements are present, then they must be written between the package statement and the class declaration. If there are no package statements, then the import statement should be the first line in the source file.

◉ Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file.

Classes have several access levels and there are different types of classes; abstract classes, final classes, etc. We will be explaining about all these in the access modifiers chapter.

Apart from the above mentioned types of classes, Java also has some special classes called Inner classes and Anonymous classes.

Java Package


In simple words, it is a way of categorizing the classes and interfaces. When developing applications in Java, hundreds of classes and interfaces will be written, therefore categorizing these classes is a must as well as makes life much easier.

Import Statements


In Java if a fully qualified name, which includes the package and the class name is given, then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.

For example, the following line would ask the compiler to load all the classes available in directory java_installation/java/io −

import java.io.*;

A Simple Case Study


For our case study, we will be creating two classes. They are Employee and EmployeeTest.

First open notepad and add the following code. Remember this is the Employee class and the class is a public class. Now, save this source file with the name Employee.java.

The Employee class has four instance variables - name, age, designation and salary. The class has one explicitly defined constructor, which takes a parameter.

Example

import java.io.*;
public class Employee {

   String name;
   int age;
   String designation;
   double salary;

   // This is the constructor of the class Employee
   public Employee(String name) {
      this.name = name;
   }

   // Assign the age of the Employee  to the variable age.
   public void empAge(int empAge) {
      age = empAge;
   }

   /* Assign the designation to the variable designation.*/
   public void empDesignation(String empDesig) {
      designation = empDesig;
   }

   /* Assign the salary to the variable salary.*/
   public void empSalary(double empSalary) {
      salary = empSalary;
   }

   /* Print the Employee details */
   public void printEmployee() {
      System.out.println("Name:"+ name );
      System.out.println("Age:" + age );
      System.out.println("Designation:" + designation );
      System.out.println("Salary:" + salary);
   }
}

As mentioned previously in this tutorial, processing starts from the main method. Therefore, in order for us to run this Employee class there should be a main method and objects should be created. We will be creating a separate class for these tasks.

Following is the EmployeeTest class, which creates two instances of the class Employee and invokes the methods for each object to assign values for each variable.

Save the following code in EmployeeTest.java file.

import java.io.*;
public class EmployeeTest {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      Employee empOne = new Employee("James Smith");
      Employee empTwo = new Employee("Mary Anne");

      // Invoking methods for each object created
      empOne.empAge(26);
      empOne.empDesignation("Senior Software Engineer");
      empOne.empSalary(1000);
      empOne.printEmployee();

      empTwo.empAge(21);
      empTwo.empDesignation("Software Engineer");
      empTwo.empSalary(500);
      empTwo.printEmployee();
   }
}

Now, compile both the classes and then run EmployeeTest to see the result as follows −

Output

C:\> javac Employee.java
C:\> javac EmployeeTest.java
C:\> java EmployeeTest
Name:James Smith
Age:26
Designation:Senior Software Engineer
Salary:1000.0
Name:Mary Anne
Age:21
Designation:Software Engineer
Salary:500.0