Home » Core Java » class » Page 4

class

Generic deep copy

This is an example of how to make a generic copy of a class, using Java reflection. In order to make a generic copy of a class we have created two classes and then copied the one to the other, as described below: We have created two classes A and B. B class has a String and an int field ...

Read More »

Avoid side-effects with Object.clone

In this example we shall show you how to avoid side-effects when using Object.clone. To avoid side-effects when using Object.clone we have performed the following steps: We have created a class, Enemy, that has a method, void mungeFunc(SideEffects sdf), where it gets an object of SideEffects class and changes its fields’ values. SideEffects class implements the Cloneable interface to indicate to the ...

Read More »

Inheritance constructor calls example

With this example we are going to demonstrate how to inherit constructor calls from a super class to a sub class. In short, to inherit constructor calls from a super class to a sub class: We have created a class A, with a constructor and a class B that extends A and also has a constructor. We have also created ...

Read More »

Initialize constructor with composition

This is an example of how to initialize a constructor with composition. We have created an object with reference to another object, as described below: We have created class A that has a String field and overrides the toString() API method of Object, where it returns its String field. We have also created a class, Composition that has four String fields, ...

Read More »

Default constructor calls superclass constructor

In this example we shall show you how to call the superclass constructor in a default class constructor. An easy way to call a superclass constructor in a class constructor is to create a class that extends another class, as shown below: We have created a class A that has a default constructor without fields. We have also created class ...

Read More »

Use serializable interface to perform deep copy

With this example we are going to demonstrate how to use the Serializable interface to perform a deep copy of a class. Classes implement the Serializable interface in order to have their state serialized or deserialized. In short, to use the Serializable interface to perform a deep copy of a class: We have created a class, Car that implements the ...

Read More »

Deep Copy example – Part 2

This is an example of how to create a deep copy of a class. In order to create a deep copy of a class we have overriden the clone() API method of Cloneable interface, as described in the following steps: We have created a class, Car that has a String field and a getter and setter method for it. It also ...

Read More »

Shallow Copy example

In this example we shall show you how to create a shallow copy of a class. To create a shallow copy of a class we have performed the following steps: We have created a class, Car that has a String field and a getter and setter method for it. It also has a constructor using its String field. We have also created ...

Read More »

Deep Copy example

With this example we are going to demonstrate how to create a deep copy of a class. In short, to create a deep copy of a class: We have created a class, Address that implements the Cloneable interface and in its overriden clone() method it uses its superclass clone(). It has String fields that uses in its constructors. In its ...

Read More »

Clonable Object example

With this example we are going to demonstrate how to create a cloneable object of a class. In short, to create a cloneable object of a class, we have created a class that implements the Cloneable interface, so that it can use the clone() method to create cloneable objects, as described in the following steps: We have created a class ...

Read More »