Sampath Kumar S Assistant Professor, SECE

Slides:



Advertisements
Similar presentations
Clonazione La clonazione... Ovvero: come costruire una copia (probabilmente che ritorni true su equals?)
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Mutable, Immutable, and Cloneable Objects Chapter 15.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Interface. Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Java Object and Class in Java. Java Naming conventions A naming convention is a rule to follow as you decide what to name your identifiers e.g. class,
CMSC 132 Week2 Lab1 Comparable vs Comparator clone() finalize()
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Object Oriented Programming Java 1 Some Notes on Cloning, Packages and Visibility Notes from Bruce Eckel’s “Thinking in Java” and “Just Java” by.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Classes Revisited Chapter 8.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes.
Staples are our staple Building upon our solution.
Chapter 7: Cloning and RTTI
Interfaces Unit 08.
Chapter 8 Classes and Objects
RADE new features via JAVA
Code Magnets problem for wk5
CIS3023: Programming Fundamentals for CIS Majors II
CSE 331 Cloning objects slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Lecture 11 C Parameters Richard Gesick.
Класс Object Макаревич Л. Г..
Conditional Statements
Introduction to Java Programming
The Boolean (logical) data type boolean
Interfaces and Constructors
Sampath Kumar S Assistant Professor, SECE
Sampath Kumar S Assistant Professor, SECE
Sampath Kumar S Assistant Professor, SECE
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
Sampath Kumar.S Assistant Professor/IT, SECE
S.VIGNESH Assistant Professor, SECE
Sampath Kumar S Assistant Professor, SECE/IT
Method Overloading in JAVA
Sampath Kumar S Assistant Professor, SECE
Method Overriding in Java
Sampath Kumar S Assistant Professor, SECE
Graphics Programming - Frames
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Why did the programmer quit his job?
Interfaces.
Sampath Kumar S Assistant Professor, SECE
Chapter 8 Class Inheritance and Interfaces
Clonazione La clonazione... Ovvero: come costruire una copia
ITM 352 Functions.
Interfaces, Enumerations, Boxing, and Unboxing
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Sampath Kumar S Assistant Professor, SECE Object Cloning Sampath Kumar S Assistant Professor, SECE

5/31/2019 Object Cloning The object cloning is a way to create exact copy of an object. clone() method of Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException. The clone() method is defined in the Object class. Syntax of the clone() method is as follows: protected Object clone() throws CloneNotSupport edException   Sampath Kumar S, AP

5/31/2019 Why use clone() method ? The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning. Sampath Kumar S, AP

Advantage of Object cloning 5/31/2019 Advantage of Object cloning Less processing task. Sampath Kumar S, AP

Example: clone() method (Object cloning) 5/31/2019 Example: clone() method (Object cloning) class Student implements Cloneable{ String rollno; String name; Student(String rollno,String name){ this.rollno=rollno; this.name=name; } public Object clone()throws CloneNotSupportedException{ return super.clone(); Sampath Kumar S, AP

5/31/2019 Cont.., public static void main(String args[]){ try{ Student s1=new Student(“12IT26”,"NIJAR"); Student s2=(Student)s1.clone(); System.out.println(s1.rollno+" "+s1.name); System.out.println(s2.rollno+" "+s2.name); } catch(CloneNotSupportedException c) {} Output: 12IT26 NIJAR 12IT26 NIJAR Sampath Kumar S, AP

Explanation: Both reference variables have the same value. 5/31/2019 Explanation: Both reference variables have the same value. Thus, the clone() copies the values of an object to another. So we don't need to write explicit code to copy the value of an object to another. If we create another object by new keyword and assign the values of another object to this one, it will require a lot of processing on this object. So to save the extra processing task we use clone() method. Sampath Kumar S, AP

5/31/2019 Sampath Kumar S, AP

5/31/2019 Thank You  Sampath Kumar S, AP