Plumbing Website Template Free Download

How to create a completely responsive Online Plumbing Website – Training Website Design Template using HTML CSS and JavaScript. Plumbing Website Template Free Download

Cloning Object

Copying an exact copy of an object to another object is called Object Cloning. Like all other objects, cloned objects are also stored in Heap memory. One of the few ways to clone an object in Java is to use a method called clone(), which is a protected method of the Object class.

An object of a class for which to call the clone() method must implement the Cloneable() interface. Otherwise the code will compile, but will throw a CloneNotSupportedException at runtime. I am giving the class the ability to clone any object by implementing the Cloneable() interface.

Since all classes extend the Object class, we can call clone() from any class. But if the object of the class that you want to clone, if that class does not implement the Cloneable() interface, then Exception (CloneNotSupportedException) occurs because the class does not have the ability to clone at runtime.

Plumbing Website Template Free Download

Now let’s see the code:

Identity Class:

public class Identity {

  String department;

  String batch;

  int rollNumber;

  public Identity(String department, String batch, int rollNumber) {

      this.department = department;

      this.batch = batch;

      this.rollNumber = rollNumber;

  }

}

Student Class:

public class Student implements Cloneable {

      Identity identity;

  String firstName;

  String lastName ;

  int age;

  Student(String firstName, String lastName, int age, Identity identity) {

      this.firstName = firstName;

      this.lastName = lastName;

      this.age = age;

      this.identity = identity;

  }

  public int getAge() {

      return age;

  }

  public String getFirstName() {

      return firstName;

  }

  public String getLastName() {

      return lastName;

  }

  public Identity getIdentity() {

      return identity;

  }

  public Object clone() throws CloneNotSupportedException {

      return super.clone();

  }

}

Main Class:

public class Main {

  public static void main(String[] args) {

      Identity identity = new Identity(“CSE”, “09”, 97);

      Student student1 = new Student(“Mukit”, “Chowdhury”, 27, identity);

      try {

          Student student2= (Student) student1.clone();

          System.out.println(“Name: ” + student2.getFirstName() + ” ” + student2.getLastName());

          System.out.println(“Batch: ” + student2.getIdentity().batch + “, Age: ” + student2.age

                  + “, Department: ” + student2.getIdentity().department + “, RollNumber: “

                  + student2.getIdentity().rollNumber);

                      student1.firstName = “Rakib”;

          student1.identity.department = “EEE”;

          student1.identity.rollNumber = 56;

                      student1.age = 30;

                      System.out.println(“Name: ” + student2.getFirstName() + ” ” + student2.getLastName());

          System.out.println(“Batch: ” + student2.getIdentity().batch + “, Age: ” + student2.age

                                      + “, Department: ” + student2.getIdentity().department + “, RollNumber: “

                                      + student2.getIdentity().rollNumber);

      } catch (CloneNotSupportedException e) {

          e.printStackTrace();

      }

  }

}

A reference to the Identity class is declared as a member variable of the Student class. And in the main method of the Main class, an object of the Student class is created and that object is cloned.

The cloning I have done here is called Shallow Cloning. In this cloning, if any member variable of the original object is a reference to another object, then only the reference is copied. As a result, if any member variable of the original object (student1 in the above example) is changed by that reference (in this case Identity), its affect is also read in the cloned object (student2 in the example).

And if the member variable is a primitive data type, then the value of the variable is copied as the value of that member variable of the cloned object.

This means that if the member variable is primitive then it is a behavior and if it is a reference

Another way, what is the reason? I will answer it later!

There is another type of cloning in Java, which is called Deep Cloning. In this cloning all member variables are independent in both parent object and cloned object. That is, no matter which one of these two objects I change, the other will have no affect.

Well, now the answer to the previous question! In shallow cloning, each object is copied bit-by-bit to another object. So it copies the value of the member variable it gets. Now the value for the reference type is actually a reference of address. So when copying bit-by-bit, this reference of address is also copied as a reference to the cloned object. As a result, both references are now referring to the same address. Due to which, if the member variable reference changes in any one of these two objects, the same changes in the other one as well.

Now another question! If we run the above codes. I will see that, even if the firstName of student1 is changed, that change is not read in the firstName of student2! But firstName is String type, that is not primitive! So why not change? Figure it out! 😀

Happy Coding…

Shared Memory in C

What is Shared Memory and why?

In general, two separate applications cannot access a common memory. Sometimes, two different processes of the same program need to decide on the value of a global variable. For now, I wrote to a variable in one process, and read from that variable in another process. In general we can’t even do this simple task. Because each process has a separate address space. So even though the program is the same, we do not get the value of the same variable changed in one process in another process.

Here comes Shared Memory. The name Shared Memory shows what it actually does. Yes, Shared Memory is a “Segment of Memory” that multiple processes can share. That means if the value of a shared variable changes from a process, the process in which that variable is shared, everyone will get the updated value.

Implementation:

For now we will look at writing and reading values in the same variable from two programs. A few system calls are required before using Shared Memory:

  ftok(): Call this to create a unique key.

  shmget(): I have to call this function to get the Shared Memory Segment that I want to access.

  shmat(): Attaches the code in the process to the shared memory segment.

  shmdt(): Detaches the shared memory segment from the process.

  shmctl(): Called to destroy shared memory segment.

If we can generate the unique key in some other way, then ftok() need not be called. But remember, this key must be unique for each memory segment.

Plumbing Website Template Free Download

The shmget() function must be used for both the read process and the write process. When read, the shared memory segment has already been created by another process, so shmget() is called in the read process to get the created shared memory segment. This is done by passing the third parameter (int shmflg) null when calling shmget(). Then the system understands that shmget() has been called to access any previously created Shared Memory Segment. Details can be found at the above link of shmget().

got, now I will attach the Shared Memory Segment with the running process. For this call shmat().

Plumbing Website Template Free Download

Before Download

You must Join our Facebook Group and Subscribe YouTube Channel

All Links in Below:






Join Our FreeWebsiteCreate Facebook Group to get an instant update for projects, templates, design resources, and solutions.

Join Our YouTube Channel & Subscribe with Bell Icon for New Video:

Join Our Official Facebook Page For the Latest updates All Code Projects are Free:

Visit our service page to get premium services.

Free Website Create – HTML CSS, PHP, JavaScript Programming Projects For Free

Follow Us

Thank You,

Stay with FreeWebsiteCreate.net

Share the post if necessary.

Before Download

You must Join our Facebook Group and Subscribe YouTube Channel




FreeWebsiteCreate.net tries to provide HTML, CSS, SCSS, JavaScript, React, Android Studio, Java, PHP, Laravel, Python, Django, C#(C Sharp), and ASP.net-related projects 100% free. We try to make learning easier. Free Website Create always tries to give free projects to new learners. Free projects and source code will help to learn quickly.

They can save time and learn more. In this post, we share a free portfolio project website code with HTML and CSS. This free code portfolio contains a single landing page with a responsive design. In this post, we get a free best carpenter and craftsman service website designed by FreeWebsiteCreate with HTML, CSS, Bootstrap, and JavaScript.

To get a free website project code,

Keep bookmarking our website, Save categories links, Follow Social Media, Follow the youtube channel Join Facebook groups.

Stay with FreeWebsiteCreate.net

Share the post if necessary.

Leave a Comment