0% found this document useful (0 votes)
22 views7 pages

Packages

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

Packages

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

Abstract Class in Java

Java abstract class is a class that cannot be initiated by itself, it needs to be


sub-classed by another class to use its properties. An abstract class is
declared using the “abstract” keyword in its class definition.

Key Points to Remember

The important rules that we need to follow while using an abstract class in Java are as follows:

 The keyword "abstract" is mandatory while declaring an abstract class in


Java.
 Abstract classes cannot be instantiated directly.
 An abstract class must have at least one abstract method.
 An abstract class includes final methods.
 An abstract class may also include non-abstract methods.
 An abstract class can consist of constructors and static method.

Example:
Understanding the real scenario of Abstract class

In this example, Shape is the abstract class, and its implementation is


provided by the Rectangle and Circle classes.

abstract class Shape{

abstract void draw();

//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 Circle extends Shape{

void draw(){System.out.println("drawing circle");}

//In real scenario, method is called by programmer or user

class TestAbstraction{

public static void main(String args[]){

Shape s=new Circle(); //In a real scenario, object is provided


through

method, e.g., getShape() method

s.draw();
}

Difference between abstract class and interface


Abstract class and interface both are used to achieve abstraction where we
can declare the abstract methods. Abstract class and interface both can't be
instantiated.

But there are many differences between abstract class and interface that are
given below.

Abstract class Interface

1) Abstract class can have abstract Interface can have only abstract methods.
and non-abstract methods. Since Java 8, it can have default and static
methods also.

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


multiple inheritance.

3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.

4) Abstract class can provide the Interface can't provide the


implementation of interface. implementation of abstract class.

5) The abstract keyword is used to The interface keyword is used to declare


declare abstract class. interface.

6) An abstract class can extend An interface can extend another Java


another Java class and implement interface only.
multiple Java interfaces.

7) An abstract class can be extended An interface can be implemented using


using keyword "extends". keyword "implements".

8) A Java abstract class can have class Members of a Java interface are public by
members like private, protected, etc. default.

9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

Packages in Java
A java package is a group of similar types of classes, interfaces and sub-
packages. Package in java can be categorized into two types i.e. built-in
package and user-defined package.

Types of packages:

Built-in Packages
These packages consist of a large number of classes which are a part of
Java API. Some of the commonly used built-in packages 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.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for
graphical user interfaces (like button , ;menus etc).
6) java.net: Contain classes for supporting networking operations.

User-defined packages
These are the packages that are defined by the user. First we create a
directory myPackage Then create the MyClass inside the directory with the first
statement being the package names. Inside a package, you can have Java files
like classes, interfaces, and a package as well (called a sub-package).

Example:

import java.lang.*;

public class Example {

public static void main(String[] args) {

double radius = 5.0;

double area = Math.PI * Math.pow(radius, 2);

System.out.println("Area: " + area);

String message = "Hello, World!";

int length = message.length();

System.out.println("Length: " + length);

int number = 42;

String binary = Integer.toBinaryString(number);

System.out.println("Binary: " + binary);


}

Output:

Area: 78.53981633974483

Length: 13

Binary: 101010

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be
easily

maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

You might also like