0% found this document useful (0 votes)
2 views6 pages

Package and Polymorphism

Computer science

Uploaded by

safiyapathan2004
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)
2 views6 pages

Package and Polymorphism

Computer science

Uploaded by

safiyapathan2004
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/ 6

Java Packages

Java Packages & API


A package in Java is used to group related classes. Think of it as a folder in
a file directory. We use packages to avoid name conflicts, and to write a
better maintainable code. Packages are divided into two categories:

 Built-in Packages (packages from the Java API)


 User-defined Packages (create your own packages)

Built-in Packages
The Java API is a library of prewritten classes, that are free to use, included in
the Java Development Environment.

The library contains components for managing input, database programming,


and much much more. The complete list can be found at Oracles website:
The library is divided into packages and classes. Meaning you can either
import a single class (along with its methods and attributes), or a whole
package that contain all the classes that belong to the specified package.

To use a class or a package from the library, you need to use


the import keyword:

Syntax
import package.name.Class; // Import a single class

import package.name.*; // Import the whole package

Import a Class
If you find a class you want to use, for example, the Scanner class, which is
used to get user input, write the following code:

Example
import java.util.Scanner;

In the example above, java.util is a package, while Scanner is a class of


the java.util package.

To use the Scanner class, create an object of the class and use any of the
available methods found in the Scanner class documentation. In our example,
we will use the nextLine() method, which is used to read a complete line:

Example
Using the Scanner class to get user input:

import java.util.Scanner;

class MyClass {

public static void main(String[] args) {

Scanner myObj = new Scanner(System.in);

System.out.println("Enter username");

String userName = myObj.nextLine();

System.out.println("Username is: " + userName);

Enter user name

Hamsha
Import a Package
There are many packages to choose from. In the previous example, we used
the Scanner class from the java.util package. This package also contains
date and time facilities, random-number generator and other utility classes.

To import a whole package, end the sentence with an asterisk sign ( *). The
following example will import ALL the classes in the java.util package:

Example
import java.util.*;

User-defined Packages
To create your own package, you need to understand that Java uses a file
system directory to store them. Just like folders on your computer:

Example
└── root
└── mypack
└── MyPackageClass.java

To create a package, use the package keyword:

MyPackageClass.java
package mypack;

class MyPackageClass {

public static void main(String[] args) {

System.out.println("This is my package!");

}
Java Polymorphism

Java Polymorphism
Polymorphism means "many forms", and it occurs when we have many
classes that are related to each other by inheritance.

Inheritance lets us inherit attributes and methods from another


class. Polymorphism uses those methods to perform different tasks. This
allows us to perform a single action in different ways.

For example, think of a superclass called Animal that has a method


called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds -
And they also have their own implementation of an animal sound (the pig
oinks, and the cat meows, etc.):

Example
class Animal {

public void animalSound() {

System.out.println("The animal makes a sound");

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

class Dog extends Animal {

public void animalSound() {


System.out.println("The dog says: bow wow");

Remember from the Inheritance that we use the extends keyword to inherit
from a class.

Now we can create Pig and Dog objects and call the animalSound() method on
both of them:

Example
class Animal {

public void animalSound() {

System.out.println("The animal makes a sound");

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

class Dog extends Animal {

public void animalSound() {

System.out.println("The dog says: bow wow");

class Main {

public static void main(String[] args) {


Animal myAnimal = new Animal(); // Create a Animal object

Animal myPig = new Pig(); // Create a Pig object

Animal myDog = new Dog(); // Create a Dog object

myAnimal.animalSound();

myPig.animalSound();

myDog.animalSound();

Why And When To Use "Inheritance" and "Polymorphism"?


- It is useful for code reusability: reuse attributes and methods of an existing
class when you create a new class.

W3schools Pathfinder

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy