Unit-2 in Simple Way
Unit-2 in Simple Way
1. What is meant by data abstraction? How to achieve data abstraction(abstract classes and
interfaces) in java. How to inherit an extend interface to another?(V.imp) (or)
2. What is interface? Write a java program to illustrate the use of interface(V.imp)
Abstraction
Abstraction in Java is a process of hiding the implementation details from the user and showing
only the functionality to the user.
Abstraction in Java can be achieved using
a. Abstract classes
In Java a class preceded by abstract keyword is called abstract class.
An abstract class may or may not have abstract methods.
We cannot create object for abstract class.
It is used to achieve abstraction but it does not provide 100% abstraction because it can have
concrete methods.
It can have abstract and non-abstract methods.
Abstract methods should be present inside abstract class only.
Abstract class need to be inherited by other class (extended) and its abstract methods need to
be overridden. Hence in abstract class only the abstract methods are overridden.
Abstract class can contain constructors and static methods too but they are not declared
abstract.
Note : Remember that abstract class cannot be instantiated , i.e. we can’t create an object of an
abstract class , but can only create a reference variable to refer to other class object which is
helpful in run-time polymorphism.
o Syntax : abstract class class_name {
}
AbstractClassExample
class AbstractClassEx
{
public static void main(String[] args)
{
animal r=new Dog(); // superclass has capability of storing childclass objects
}
}
Abstract method
Method that are declared without any body within an abstract class are called abstract method.
Syntax: abstract return_type function_name (); //No definition
The method body will be defined by its subclass.
Abstract method can never be final and static. Any class that extends an abstract class must
implement all the abstract methods.
AbstractMethodExample
class AbstractMethodEx
{
public static void main(String[] args)
{
Dog d=new Dog();
d.sound();
Lion l=new Lion();
l.sound();
}
}
b. Interfaces
Interface in Java is the way to achieve complete abstraction. Also interface in Java is used to
achieve multiple inheritance by implementing interfaces.
Interface is similar to class and contains only public, abstract methods and public, static and final
fields. Interfaces differ from class in the way that interface cannot contains constructors and non
– abstract methods. Also interfaces cannot be instantiated.
Interfaces should be implemented by using implements The class that implements the interface
should define all the methods of interface and the method should be declared public.
Interface only tells what a class contains and the behavior is defined in the class that implements
it. Advantages
Reduces complexity, Avoid code duplication, Eases the burden of maintenance,Increase security
and confidentiality
Interface Example
import java.util.Scanner;
interface client
{
void input(); //public and abstract methods
void output();
}
class Customer implements client
{
String name; double salary;
public void input()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter your name:");
name=s.nextLine();
System.out.println("Enter your salary:");
salary=s.nextInt();
}
public void output()
{
System.out.println("Enter your name:"+name);
System.out.println("Enter your salary:"+salary);
}
}
class InterfacesEx
{
public static void main(String[] args)
{
client c=new Customer();
c.input();
c.output();
}
}
3. Define a package. How to import packages? Explain with illustrations. (V.imp)
Packages in java are like container which keeps classes, interfaces and sub-packages in an
organized way. You can visualize packages as folders in your computer. Folder houses sub-folders
and other files.
Similarly, Java package contains collection of related classes, interfaces and sub-packages.
Advantages:
The main objectives of packages are:
a. To resolve name confects.
b. To improve modularity of the application.
c. To provide security.
d. Easy to locate the related classes
e. Reuse the existing classes in packages
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Some of the existing packages in Java are −
1. java.lang: Contains language support classes(e.g classed which defines primitive data
types,math operations). This package is automatically imported.
2. java.io: Contains classed for supporting input / output operations.
3. java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support; for Date / Time operations.
Import statement
import: This is the Java keyword used to indicate that you are importing a class or
package.
packageName: This is the name of the package containing the class you want to import.
ClassName: This is the name of the class (or other type) you want to import from the
specified package.
the import statement is used to bring classes, interfaces, and other types defined in
other packages into the current file's scope.
It simplifies the usage of classes from different packages by allowing you to refer to
them directly by their simple names, rather than their fully qualified names (which
include the package name).
Syntax: import java.packageName.ClassName;
package com.datasciencea;
class Packages
{
public static void main(String[] args)
{
System.out.println("Package demonstration");
}
}
compiling Java programs Inside packages
javac -d . Packages.java
Executing java Package file
Fully qualified path we should write inorder to execute.
java com/datasciencea/Packages
a. Purpose: The primary purpose of CLASSPATH is to tell the JVM where to find user-
defined classes and resources that are not part of the standard Java library.
b. Search Order: When a Java program attempts to load a class, the JVM looks for it in the
directories and JAR files specified in the CLASSPATH. It searches for classes in the order
they are listed in the CLASSPATH.
c. Default Value: If you do not explicitly set the CLASSPATH, the JVM uses a default
classpath, which includes the current directory (.). However, it's considered a best
practice to set the CLASSPATH explicitly for your application to avoid unexpected
behavior.
d. Setting CLASSPATH: You can set the CLASSPATH in several ways:
Environment Variable: You can set it as an environment variable on your system.
5. Demonstrate about File Input Stream and File Output Stream (V.imp)
In Java, FileInputStream and FileOutputStream are classes that allow you to read data from a file
(input) and write data to a file (output), respectively. They are part of the java.io package and are
commonly used for working with files. Below, I'll provide examples of how to use
FileInputStream and FileOutputStream to read from and write to a file.
import java.io.FileInputStream;
import java.io.IOException;
// Read bytes from the file until the end is reached (-1 indicates end of file)
while ((byteData = fis.read()) != -1) {
System.out.print((char) byteData); // Convert byte to char and print
}
import java.io.FileOutputStream;
import java.io.IOException;
b) Auto Boxing
The process of converting a primitive type value into its corresponding wrapper class object is
called autoboxing or simply boxing. For example, converting an int value to an Integer class
object.
The compiler automatically performs the autoboxing when a primitive type value has assigned to
an object of the corresponding wrapper class.
try {
// Create a FileInputStream for the source file
FileInputStream fis = new FileInputStream(sourceFileName);
// Read data from the source file and write it to the destination file
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}