Lecture-2.3
Lecture-2.3
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Classes
2
WRAPPER CLASS
• The wrapper class in Java provides the mechanism to convert primitive into object
and object into primitive.
Need of Wrapper Classes
• They convert primitive data types into objects. Objects are needed if we wish to
modify the arguments passed into a method (because primitive types are passed by
value).
• The classes in java.util package handles only objects and hence wrapper classes help
in this case also.
• Data structures in the Collection framework, such as ArrayList and Vector, store only
objects and not primitive types.
• An object is needed to support synchronization in multithreading.
TYPES OF WRAPPER CLASS
WRAPPER CLASS: AUTOBOXING AND
UNBOXING
• Autoboxing: Automatic conversion of primitive types to the object of
their corresponding wrapper classes is known as autoboxing.
For example – conversion of int to Integer, long to Long, double to
Double etc. // Java program to demonstrate Autoboxing
• Unboxing: It is just the reverse process of autoboxing. Automatically
converting an object of a wrapper class to its corresponding primitive
type is known as unboxing.
For example – conversion of Integer to int, Long to long, Double to
double etc.
WRAPPER CLASS: AUTOBOXING
// Java program to demonstrate Autoboxing
import java.util.ArrayList;
class Autoboxing
{ public static void main(String[] args)
{
char ch = 'a';
// Autoboxing- primitive to Character object conversion
Character a = ch;
ArrayList<Integer> arrayList = new ArrayList<Integer>();
// Autoboxing because ArrayList stores only objects
arrayList.add(25);
System.out.println(arrayList.get(0));
}}
WRAPPER CLASS: UNBOXING
// Java program to demonstrate Unboxing
import java.util.ArrayList;
class Unboxing
{ public static void main(String[] args)
{
Character ch = 'a';
// unboxing - Character object to primitive conversion
char a = ch;
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(24);
// unboxing because get method returns an Integer object
int num = arrayList.get(0);
// printing the values from primitive data types
System.out.println(num); } }
INTERFACE
• An interface is a reference type in Java. It is similar to class. It is a collection of
abstract methods. A class implements an interface, thereby inheriting the
abstract methods of the interface.
• Along with abstract methods, an interface may also contain constants, default
methods, static methods, and nested types. Method bodies exist only for default
methods and static methods.
• Writing an interface is similar to writing a class. But a class describes the
attributes and behaviors of an object. And an interface contains behaviors that a
class implements.
• Unless the class that implements the interface is abstract, all the methods of the
interface need to be defined in the class.
INTERFACE
• An interface is similar to a class in the following ways −
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the name of the
interface matching the name of the file.
• The byte code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding bytecode file must be in a
directory structure that matches the package name.
INTERFACE
However, an interface is different from a class in several ways, including −
• You cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
• An interface is not extended by a class; it is implemented by a class.
• An interface can extend multiple interfaces.
INTERFACE :Declaration
• An interface is declared by using the interface keyword.
• It provides total abstraction; means all the methods in an interface are declared with
the empty body, and all the fields are public, static and final by default.
• A class that implements an interface must implement all the methods declared in the
interface.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
INTERFACE :Example
interface Drawable{
void draw();
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");} }
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");} }
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
INTERFACE :Declaration
• An interface is declared by using the interface keyword.
• It provides total abstraction; means all the methods in an interface are declared with
the empty body, and all the fields are public, static and final by default.
• A class that implements an interface must implement all the methods declared in the
interface.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
CLONEABLE INTERFACE
When you make a copy of an object reference:
– The original and copy are references to the same object.
– This means a change to either variable also affect the other.
• The clone( ) method:
– is a protected member of Object,
– can only be invoked on an object that implements Cloneable
• Object cloning performs a bit-by-bit copy.
CLONEABLE INTERFACE
Objects can be cloned only of those classes that implement the
Cloneable interface.
• The Cloneable interface has no members. It is a marker interface and is
used to indicate that a class allows a bitwise copy of an object.
• If you call clone( ) on a class that does not implement Cloneable, a
CloneNotSupportedException is thrown.
• When a clone is made, the constructor for the object being cloned is
not called.
• A clone is simply an exact copy of the original
INTERFACE vs ABSTRACT CLASS:
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since Java 8, it can
abstract methods. have default and static methods also.
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.
Video Lectures :
https://www.youtube.com/watch?v=jJ8L3SeFy_E
E-book :
https://www.tutorialspoint.com/java/java_interfaces.html
https://www.w3schools.com/java/java_wrapper_classes.as
p
THANK YOU