Type casting,Wrapper classes ,Interface

Type casting:

Convert a value from one data type to another data type is known as type casting.

Types of Type Casting:

There are two types of type casting:

  • Widening Type Casting
  • Narrowing Type Casting

Widening Type Casting:

Converting a lower data type into a higher one is called widening type casting. It is also known as implicit conversion or casting down. It is done automatically. It is safe because there is no chance to lose data.

  • Both data types must be compatible with each other.
  • The target type must be larger than the source type.
byte -> short -> char -> int -> long -> float -> double 

Narrowing Type Casting:

Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit conversion or casting up. It is done manually by the programmer. If we do not perform casting then the compiler reports a compile-time error.

double -> float -> long -> int -> char -> short -> byte

Wrapper classes in Java:

The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.

Use of Wrapper classes in Java:

Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.

  • Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
  • Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.[TBD]
  • Synchronization: Java synchronization works with objects in Multithreading.[TBD]
  • java.util package: The java.util package provides the utility classes to deal with objects.[TBD]
  • Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.[TBD]
Primitive TypeWrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Autoboxing:

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.

Unboxing:

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing.

Custom Wrapper class in Java[TBD]:

Java Wrapper classes wrap the primitive data types, that is why it is known as wrapper classes. We can also create a class which wraps a primitive data type. So, we can create a custom wrapper class in Java.

//Creating the custom wrapper class  
class Javatpoint{  
private int i;  
Javatpoint(){}  
Javatpoint(int i){  
this.i=i;  
}  
public int getValue(){  
return i;  
}  
public void setValue(int i){  
this.i=i;  
}  
@Override  
public String toString() {  
  return Integer.toString(i);  
}  
}  
//Testing the custom wrapper class  
public class TestJavatpoint{  
public static void main(String[] args){  
Javatpoint j=new Javatpoint(10);  
System.out.println(j);  
}}  

Interface :

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.It cannot be instantiated just like the abstract class.

  • we can have default and static methods in an interface.
  •  we can have private methods in an interface.

How to declare an interface?

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.

Interface <Interface Name> {

//Declare Constant Fields;

//Declare Methods;

//Default Methods; 

 }

Why use Java interface?

There are mainly three reasons to use interface. They are given below.

  • It is used to achieve abstraction.
  • By interface, we can support the functionality of multiple inheritance.
  • It can be used to achieve loose coupling.(The term Coupling describes the dependency of one class for the other. So, while using an interface, we define the method separately and the signature separately. This way, all the methods, and classes are entirely independent and archives Loose Coupling.)

Multiple inheritance in Java by interface:

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance.

 multiple inheritance in java

Interface inheritance:[TBD]

A class implements an interface, but one interface extends another interface.[TBD]

Notes on Interfaces:

  • Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to create an “Animal” object in the MyMainClass)
  • Interface methods do not have a body – the body is provided by the “implement” class
  • On implementation of an interface, you must override all of its methods
  • Interface methods are by default abstract and public
  • Interface attributes are by default publicstatic and final
  • An interface cannot contain a constructor (as it cannot be used to create objects)

Interface vs Class:

InterfaceClass
Keyword used: interfaceKeyword used: class
Interfaces do not have a constructorClass includes a constructor
Interface stores only the signature of a methodClass stores complete method definition
Interfaces do not need Access SpecifiersIn Class, Access Specifiers are mandatory
Interfaces do not include Data MembersClass includes Data Members
Interfaces do not have Static MembersClass includes Static Members

  Reference:

https://www.simplilearn.com/tutorials/java-tutorial/java-interface

https://www.programiz.com/java-programming/interfaces

https://www.javatpoint.com/wrapper-class-in-java

https://www.javatpoint.com/interface-in-java

https://www.w3schools.com/java/java_interface.asp

https://www.javatpoint.com/type-casting-in-java

Leave a comment

Design a site like this with WordPress.com
Get started