A classwhich is declared with the abstract
keyword is known as an abstract class
in Java.
It can have abstract and non-abstract
methods (method with the body).
Abstract class in Java
3.
Abstraction isa process of hiding the implementation details
and showing only functionality to the user.
Another way, it shows only essential things to the user and
hides the internal details, for example, sending SMS where you
type the text and send the message. You don't know the internal
processing about the message delivery.
Abstraction lets you focus on what the object does instead of
how it does it.
Abstraction in Java
4.
Ways to achieveAbstraction
There are two ways to achieve abstraction in java
Abstract class (0 to 100%)
Interface (100%)
5.
Abstract class inJava
A class which is declared as abstract is known as
an abstract class.
It can have abstract and non-abstract
methods.
It needs to be extended and its method
implemented.
It cannot be instantiated.
7.
Syntax
1. public abstractclass Shape{
public abstract void Draw( );
// Abstract method without definition
public void Display( ) {
System.out.println(“Display
Method of Shape class”);
}
}
8.
Example of Abstractclass that
has an abstract method
In this example, Bike is an abstract class that contains only one abstract
method run. Its implementation is provided by the Honda class.
1. abstract class Bike{
2. abstract void run();
3. }
4. class Honda4 extends Bike{
5. void run(){System.out.println("running safely");}
6. public static void main(String args[]){
7. Bike obj = new Honda4();
8. obj.run();
9. }
10. }
OUTPUT:
running safely
9.
Understanding the realscenario of
Abstract class
In this example, Shape is the abstract class, and its implementation is provided by
the Rectangle and Circle classes.
Mostly, we don't know about the implementation class (which is hidden to the end
user), and an object of the implementation class is provided by the factory method.
A factory method is a method that returns the instance of the class.
In this example, if you create the instance of Rectangle class, draw() method of
Rectangle class will be invoked.
File: TestAbstraction1.java
abstract class Shape{
abstract void draw();
}
}
10.
//In real scenario,implementation is provided by others i.e.
unknown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In a real scenario, object is provided
through method, e.g., getShape() method
s.draw();
}
OUTPUT
drawing circle
11.
Another example ofAbstract class in java
File: TestBank.java
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
OUTPUT
Rate of Interest is: 7 %
Rate of Interest is: 8 %
12.
abstract method, constructor,and even main() method.
File: TestAbstraction2.java
//Example of an abstract class that has abstract and non-abstract methods
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
bike is created
running safely..
gear changed
13.
Rule: If thereis an abstract method in
a class, that class must be abstract.
class Bike12{
abstract void run();
}
OUTPUT
compile time error
14.
The wrapperclass in Java provides the mechanism to convert primitive
into object and object into primitive.
Since J2SE 5.0, autoboxing and unboxing feature convert primitives
into objects and objects into primitives automatically.
The automatic conversion of primitive into an object is known as
autoboxing and vice-versa unboxing.
Wrapper classes in Java
15.
Use of Wrapperclasses 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.
o 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.
o 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.
o Synchronization: Java synchronization works with objects in Multithreading.
o java.util package: The java.util package provides the utility classes to deal with
objects.
o 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
16.
The eight classesof the java.lang package are known as wrapper
classes in Java. The list of eight wrapper classes are given below:
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
17.
Autoboxing
The automatic conversionof 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.
Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the
primitive into objects.
Wrapper class Example: Primitive to Wrapper
//Java program to convert primitive into objects
//Autoboxing example of int to Integer
public class WrapperExample1{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);//converting int into Integer explicitly
Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a+" "+i+" "+j);
}}
Output:
20 20 20
18.
Unboxing
The automaticconversion of wrapper type into its corresponding primitive type is
known as unboxing.
It is the reverse process of autoboxing.
Since Java 5, we do not need to use the intValue() method of wrapper classes to
convert the wrapper type into primitives
Wrapper class Example: Wrapper to Primitive
//Java program to convert object into primitives
//Unboxing example of Integer to int
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int explicitly
int j=a;//unboxing, now compiler will write a.intValue() internally
System.out.println(a+" "+i+" "+j);
}}
Output:
3 3 3
19.
Java Wrapper classesExample
//Java Program to convert all primitives into its corresponding
//wrapper objects and vice-versa
public class WrapperExample3{
public static void main(String args[]){
byte b=10;
short s=20;
int i=30;
long l=40;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;