[go: up one dir, main page]

0% found this document useful (0 votes)
73 views21 pages

Lecture-2.3

The document discusses Java classes, specifically wrapper classes and interfaces. It provides 3 key points: 1) Wrapper classes allow primitive data types to be converted to objects and vice versa through autoboxing and unboxing. This allows primitives to be used in collections and with object-oriented features like inheritance. 2) Interfaces define behaviors for classes to implement, containing only abstract method signatures. Classes implement interfaces to provide method bodies. Interfaces support multiple inheritance. 3) The Cloneable interface is used to indicate a class allows object cloning through the clone() method, performing a bit-by-bit copy of the object. Classes must implement Cloneable to be cloned.

Uploaded by

gdgd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views21 pages

Lecture-2.3

The document discusses Java classes, specifically wrapper classes and interfaces. It provides 3 key points: 1) Wrapper classes allow primitive data types to be converted to objects and vice versa through autoboxing and unboxing. This allows primitives to be used in collections and with object-oriented features like inheritance. 2) Interfaces define behaviors for classes to implement, containing only abstract method signatures. Classes implement interfaces to provide method bodies. Interfaces support multiple inheritance. 3) The Cloneable interface is used to indicate a class allows object cloning through the clone() method, performing a bit-by-bit copy of the object. Classes must implement Cloneable to be cloned.

Uploaded by

gdgd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Java Classes

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• Wrapper Class
• Interfaces

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.

2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.


3) Abstract class can have final, non-final, static and non- Interface has only static and final variables.
static variables.
4) Abstract class can provide the implementation of Interface can't provide the implementation of abstract
interface. class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and An interface can extend another Java interface only.
implement multiple Java interfaces.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like private, Members of a Java interface are public by default.
protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
QUIZ:

Which keyword is used to declare an interface in


java?
A. class
B. interface
C. implements
D. Abstract
QUIZ:
Q2:
Java interface is used to
A. Implement behaviour of multiple inheritance
B. Achieve abstraction
C. achieve loos coupling
D. All of the above
Summary:

In this session, you were able to :


• Learn about Wrapper classes and Interfaces
• Understand abstraction vs interface with example
References:

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

You might also like