0% found this document useful (0 votes)
13 views17 pages

Unit 5 1

Java pdf

Uploaded by

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

Unit 5 1

Java pdf

Uploaded by

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

Abstract Classes

classes become more general and probably more abstract. At some point, the
ancestor class becomes so general that you think of it more as a basis for other
classes than as a class with specific instances you want to use. Consider, for
example, an extension of our Employee class hierarchy. An employee is a person,
and so is a student. Let us extend our class hierarchy to include classes Person and
Student

Why bother with so high a level of abstraction? There are some attributes that make
sense for every person, such as name. Both students and employees have names,
and introducing a common superclass lets us factor out the getName method to a
higher level in the inheritance hierarchy.

Now let’s add another method, getDescription, whose purpose is to return a brief
description of the person, such as

an employee with a salary of $50,000.00


a student majoring in computer science

It is easy to implement this method for the Employee and Student classes. But what
information can you provide in the Person class? The Person class knows nothing
about the person except the name. Of course, you could implement
Person.getDescription() to return an empty string. But there is a better way. If you
use the abstract keyword, you do not need to implement the method at all.
public abstract String getDescription();
// no implementation required
For added clarity, a class with one or more abstract methods must itself be declared
abstract.
abstract class Person
{...
public abstract String getDescription();
}

In addition to abstract methods, abstract classes can have fields and concrete
methods. For example, the Person class stores the name of the person and has a
concrete method that returns it.
abstract class Person
{
private String name;
public Person(String n)
{
name = n;
}
public abstract String getDescription();
public String getName()
{
return name;
}
}

Abstract methods act as placeholders for methods that are implemented in the
subclasses. When you extend an abstract class, you have two choices. You can
leave some or all of the abstract methods undefined; then you must tag the subclass
as abstract as well. Or you can define all methods, and the subclass is no longer
abstract.

For example, we will define a Student class that extends the abstract Person class
and implements the getDescription method. None of the methods of the Student
class are abstract, so it does not need to be declared as an abstract class.
A class can even be declared as abstract even though it has no abstract methods.
Abstract classes cannot be instantiated. That is, if a class is declared as abstract, no
objects of that class can be created. For example, the expression

new Person("Vince Vu")

is an error. However, you can create objects of concrete subclasses.


Note that you can still create object variables of an abstract class, but such a
variable must refer to an object of a nonabstract subclass. For example:

Person p = new Student("Vince Vu", "Economics");

Here p is a variable of the abstract type Person that refers to an instance of the non
abstract subclass Student.

A C++ class is abstract if it has at least one pure virtual function.


In C++, there is no special keyword to denote abstract classes.

Let us define a concrete subclass Student that extends the abstract class
Person:
class Student extends Person
{
private String major;
public Student(String n, String m)
{
super(n);
major = m;
}
public String getDescription()
{
return "a student majoring in " + major;
}
}

The Student class defines the getDescription method. Therefore, all methods in
the Student class are concrete, and the class is no longer an abstract
class.
package abstractClasses;

public abstract class Person


{
public abstract String getDescription();
private String name;

public Person(String n)
{
name = n;
}

public String getName()


{
return name;
}
}

package abstractClasses;

import java.util.Date;
import java.util.GregorianCalendar;

public class Employee extends Person


{
private double salary;
private Date hireDay;

public Employee(String n, double s, int year, int month, int day)


{
super(n);
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year,
month -1, day);
hireDay = calendar.getTime();
}
public double getSalary()
{
return salary;
}

public Date getHireDay()


{
return hireDay;
}

public String getDescription()


{
return String.format("an employee with a salary of $%.2f",
salary);
}

public void raiseSalary(double byPercent)


{
double raise = salary * byPercent / 100;
salary += raise;
}
}

package abstractClasses;

public class Student extends Person


{
private String major;

public Student(String n, String m)


{
// pass n to superclass constructor
super(n);
major = m;
}

public String getDescription()


{
return "a student majoring in " + major;
}
}

public class PersonTest


{
public static void main(String[] args)
{
Person[] people = new Person[2];

// fill the people array with Student and Employee objects


people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
people[1] = new Student("Maria Morris", "computer science");

// print out names and descriptions of all Person objects


for (Person p : people)
System.out.println(p.getName() + ", " + p.getDescription());
}
}

Abstract class in Java


A class which 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).
Abstraction in Java

Abstraction is a 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.

Ways to achieve Abstraction

There are two ways to achieve abstraction in java

1. Abstract class (0 to 100%)


2. Interface (100%)

Abstract class in Java

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.

Points to Remember

 An abstract class must be declared with an abstract keyword.


 It can have abstract and non-abstract methods.
 It cannot be instantiated.
 It can have constructors and static methods also.
 It can have final methods which will force the subclass not to change the body of the method.

Example of abstract class

1. abstract class A{}

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an abstract method.

1. abstract void printStatus();//no method body and abstract

Example of Abstract class 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.

abstract class Bike{


abstract void run();
}
class Honda4 extends Bike{
void run()
{
System.out.println("running safely");
}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

Another example of Abstract class in java

1. abstract class Bank{


2. abstract int getRateOfInterest();
3. }
4. class SBI extends Bank{
5. int getRateOfInterest(){return 7;}
6. }
7. class PNB extends Bank{
8. int getRateOfInterest(){return 8;}
9. }
10.
11. class TestBank{
12. public static void main(String args[]){
13. Bank b;
14. b=new SBI();
15. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
16. b=new PNB();
17. System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
18. }}

Interface in Java

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.

In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
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.
Another real scenario of abstract class

The abstract class can also be used to provide some implementation of the interface. In such
case, the end user may not be forced to override all the methods of the interface.

interface A{

void a();

void b();

void c();

void d();

abstract class B implements A{

public void c(){System.out.println("I am c");}

class M extends B{

public void a(){System.out.println("I am a");}

public void b(){System.out.println("I am b");}


public void d(){System.out.println("I am d");}

class Test5{

public static void main(String args[]){

A a=new M();

a.a();

a.b();

a.c();

a.d();

Output:I am a
I am b
I am c
I am d
Encapsulation in Java
Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a
capsule which is mixed of several medicines.

We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use
setter and getter methods to set and get the data in it.

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together
code and the data it manipulates. Another way to think about encapsulation is, that it is a protective shield that
prevents the data from being accessed by the code outside this shield.

 Technically in encapsulation, the variables or data of a class is hidden from any other class and can be
accessed only through any member function of its own class in which it is declared.

 As in encapsulation, the data in a class is hidden from other classes using the data hiding concept which
is achieved by making the members or methods of a class private, and the class is exposed to the end-
user or the world without providing any details behind implementation using the abstraction concept, so
it is also known as a combination of data-hiding and abstraction.

 Encapsulation can be achieved by Declaring all the variables in the class as private and writing public
methods in the class to set and get the values of variables.

 It is more defined with the setter and getter method.

Advantages of Encapsulation

 Data Hiding: it is a way of restricting the access of our data members by hiding the implementation details.
Encapsulation also provides a way for data hiding. The user will have no idea about the inner implementation of
the class. It will not be visible to the user how the class is storing values in the variables. The user will only know
that we are passing the values to a setter method and variables are getting initialized with that value.

 Increased Flexibility: We can make the variables of the class read-only or write-only depending on our
requirements. If we wish to make the variables read-only then we have to omit the setter methods like
setName(), setAge(), etc. from the above program or if we wish to make the variables write-only then we have
to omit the get methods like getName(), getAge(), etc. from the above program

 Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.

 Testing code is easy: Encapsulated code is easy to test for unit testing.

 Freedom to programmer in implementing the details of the system: This is one of the major advantage of
encapsulation that it gives the programmer freedom in implementing the details of a system. The only
constraint on the programmer is to maintain the abstract interface that outsiders see.

Disadvantages of Encapsulation in Java

 Can lead to increased complexity, especially if not used properly.

 Can make it more difficult to understand how the system works.


 May limit the flexibility of the implementation.

Read-Only class
//A Java class which has only getter methods.
public class Student{
//private data member
private String college="AKG";
//getter method for college
public String getCollege(){
return college;
}
}

Now, you can't change the value of the college data member which is "AKG".

1. s.setCollege("KITE");//will render compile time error

Write-Only class
//A Java class which has only setter methods.
public class Student{
//private data member
private String college;
//getter method for college
public void setCollege(String college){
this.college=college;
}
}

Now, you can't get the value of the college, you can only change the value of college data member.

1. System.out.println(s.getCollege());//Compile Time Error, because there is no such method


2. System.out.println(s.college);//Compile Time Error, because the college data member is private.
3. //So, it can't be accessed from outside the class

Object Wrappers and Autoboxing

Occasionally, you need to convert a primitive type like int to an object. All primitive types
have class counterparts. For example, a class Integer corresponds to the primitive type int.
These kinds of classes are usually called wrappers. The wrapper classes have obvious
names: Integer, Long, Float, Double, Short, Byte, Character, Void, and Boolean. (The first six inherit
from the common superclass Number.) The wrapper classes are immutable—you cannot
change a wrapped value after the wrapper has been constructed. They are also final, so
you cannot subclass them.
Suppose we want an array list of integers. Unfortunately, the type parameter inside the
angle brackets cannot be a primitive type. It is not possible to form an ArrayList<int>. Here,
the Integer wrapper class comes in. It is OK to declare an array list of Integer objects.
ArrayList<Integer> list = new ArrayList<>();

Another Java SE 5.0 innovation makes it easy to add and get array elements. The call
list.add(3); is automatically translated to
list.add(Integer.valueOf(3));
This conversion is called autoboxing.

Conversely, when you assign an Integer object to an int value, it is automatically


unboxed. That is, the compiler translates
int n = list.get(i);
into
int n = list.get(i).intValue();
Automatic boxing and unboxing even works with arithmetic expressions. For example,
you can apply the increment operator to a wrapper reference:
Integer n = 3;
n++;

The compiler automatically inserts instructions to unbox the object, increment the
resulting value, and box it back.
In most cases, you get the illusion that the primitive types and their wrappers are one and
the same. There is just one point in which they differ considerably: identity. As you
know, the == operator, applied to wrapper objects, only tests whether the objects have
identical memory locations. The following comparison would therefore probably fail:
Integer a = 1000;
Integer b = 1000;
if (a == b) . . .

Finally, let us emphasize that boxing and unboxing is a courtesy of the compiler, not the
virtual machine. The compiler inserts the necessary calls when it generates the bytecodes
of a class. The virtual machine simply executes those bytecodes.

To convert a string to an integer, use the following statement:


int x = Integer.parseInt(s);
This has nothing to do with Integer objects—parseInt is a static method. But the Integer
class was a good place to put it.
Wrapper classes in Java
The wrapper class 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.
The eight classes of the java.lang package are known as wrapper classes in
Java. The list of eight wrapper classes are given below:

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.

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 Ex
{
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

Unboxing
The automatic conversion 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 Ex
{
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:
333

Java Wrapper classes Example


//Java Program to convert all primitives into its corresponding
//wrapper objects and vice-versa
public class Ex
{
public static void main(String args[])
{
byte b=10;
short s=20;
int i=30;
long l=40;
foat f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;
//Autoboxing: Converting primitives into objects
Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float foatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;

//Autoboxing: Converting primitives into objects


Byte byteobj=b;
Short shortobj=s;
Integer intobj=i;
Long longobj=l;
Float foatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;
//Printing objects
System.out.println("---Printing object values---");
System.out.println("Byte object: "+byteobj);
System.out.println("Short object: "+shortobj);
System.out.println("Integer object: "+intobj);
System.out.println("Long object: "+longobj);
System.out.println("Float object: "+foatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);
//Unboxing: Converting Objects to Primitives
byte bytevalue=byteobj;
short shortvalue=shortobj;
int intvalue=intobj;
long longvalue=longobj;
foat foatvalue=foatobj;
double doublevalue=doubleobj;
charboolean boolvalue=boolobj;
//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("byte value: "+bytevalue);
System.out.println("short value: "+shortvalue);
System.out.println("int value: "+intvalue);
System.out.println("long value: "+longvalue);
System.out.println("foat value: "+foatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}
}
Output:
---Printing object values---
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
---Printing primitive values---
byte value: 10
short value: 20
int value: 30
long value: 40foat value: 50.0
double value: 60.0
char value: a
boolean value: true

You might also like