Java unit-3
Java unit-3
INTERNET PROGRAMMING
UNIT-3
SYLLABUS:
DEFINING A SUBCLASS :
Subclass is a class which inherits the other class. It is also called a derived
class, extended class, or child class.
Syntax:
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality.
Example:
class Employee{
float salary=40000;
int bonus=10000;
}
3
Output:
SUBCLASS CONSTRUCTOR:
A subclass inherits all the members (fields, methods, and nested classes) from
its superclass. Constructors are not members, so they are not inherited by subclasses,
but the constructor of the superclass can be invoked from the subclass.
● Constructors in Java are used to initialize the values of the attributes of the
object serving the goal to bring Java closer to the real world.
Example:
// Class 1
// Super class
class Base {
Base()
// Print statement
System.out.println(
// Class 2
4
// Sub class
// Constructor of subclass
Derived()
// Print statement
System.out.println(
// Class 3
// Main class
class GFG {
// will be called
Output
MULTILEVEL INHERITANCE:
For example;
if there is a class A that extends class B and class B extends from another class C, then
this scenario is known to follow Multi-level Inheritance.
Example:
class Animal{
void eat(){System.out.println("eating...");}
void bark(){System.out.println("barking...");}
void weep(){System.out.println("weeping...");}
class TestInheritance2{
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating…
HIERARCHICAL INHERITANCE:
Example:
7
class A {
class B extends A {
class C extends A {
class D extends A {
// Driver Class
obj_B.print_A();
obj_B.print_B();
obj_C.print_A();
obj_C.print_C();
obj_D.print_A();
obj_D.print_D();
Output:
Class A
Class B
Class A
Class C
Class A
Class D
DEFINING INTERFACE:
● An Interface in Java programming language is defined as an abstract type used to
specify the behavior of a class.
● An interface in Java is a blueprint of a behavior.
● A Java interface contains static constants and abstract methods.
interface {
// by default.
A class can extend another class similar to an interface can extend another interface. But only
a class can extend to another interface, and vice-versa is not allowed.
9
Example:
interface Polygon {
class Main {
r1.getArea(5, 6);
}
10
Output:
EXTENDING INTERFACE:
● An interface can extend another interface in the same way that a class can
extend another class.
● The extends keyword is used to extend an interface, and the child interface
inherits the methods of the parent interface.
Example:
interface A {
void funcA();
interface B extends A {
void funcB();
11
class C implements B {
System.out.println("This is funcA");
System.out.println("This is funcB");
obj.funcA();
obj.funcB();
Output:
This is funcA
This is funcB
IMPLEMENTING INTERFACE:
● In java, an interface is implemented by a class.
● The class that implements an interface must provide code for all the methods
defined in the interface, otherwise, it must be defined as an abstract class.
● Like abstract classes, we cannot create objects of interfaces.
Syntax :
...
boby-of-the-class
...
Example:
interface Human {
void work();
System.out.println("Develop applications");
}
13
trainee.learn("coding");
trainee.work();
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:
3) java.util:
Contains utility classes which implement data structures like Linked List, Dictionary
and support ; for Date / Time operations.
4) java.applet:
5) java.awt:
Contain classes for implementing the components for graphical user interfaces (like
button , menus etc).
6) java.net:
User-defined packages:
These are the packages that are defined by the user. First we create a directory
myPackage (name should be same as the name of the package). Then create the
MyClass inside the directory with the first statement being the package names.
16
package myPackage;
System.out.println(s);
Important points:
CREATING A PACKAGE:
In Java, a package is a group of classes, interfaces, enumeration, and annotations.
Java contains many predefined packages such as java.lang, java.io, java.net, etc.
1. The package makes the search easier for the classes and interfaces.
2. It provides a fully qualified name that avoids naming conflicts.
3. It also controls access.
4. It organizes classes in a folder structure.
5. It improves code reusability.
17
the syntax :-
"package packageName;"
18
declare it public.
subdirectory created.
//save as Simple.java
package mypack;
System.out.println("Welcome to package");
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
19
1) Using packagename.*
● If you use a package.* then all the classes and interfaces of this package will
be accessible but not subpackages.
● The import keyword is used to make the classes and interface of another
package accessible to the current package.
1. //save by A.java
package pack;
public class A{
● //save by B.java
package mypack;
20
import pack.*;
class B{
obj.msg();
Output:Hello
2) Using packagename.classname:
If you import package.classname then only the declared class of this package will be
accessible.
● //save by A.java
package pack;
public class A{
● //save by B.java
package mypack;
import pack.A;
class B{
obj.msg();
21
Output:Hello
If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified name
every time when you are accessing the class or interface.
● It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
● //save by A.java
package pack;
public class A{
● //save by B.java
package mypack;
class B{
obj.msg();
}
Output:Hello
22
package p1
public Class A
// body of A
The package p1 contains one public class by name A. Suppose we want to add another
class B to this package. This can be done as follows :
Package p1 ;
Package p1
Public Class B
// body of B
4. Compile B.java file. This will create a B.class file and place it in the directory
p1.
Now , the package p1 will contain both the classes A and B . The statement like
import p1. * ;
● Syntax:
import packageName.ClassName;
Example:
package myPackage;
import java.util.Scanner;
int i = read.nextInt();
HIDING CLASSES :
● When we want to import a package using
import packagename.*;
● If we want to hide classes from accessing from outside of the package ,then
such classes should be declared “not public”.
public class X
{ //body of class }
import p1.*;
CLASS PATH:
● But when we want save our class file any other directory
CLASS PATH: