07 Java Fundamental Package, Interface
07 Java Fundamental Package, Interface
Package in Java
Package 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.
Advantage of Package
Package is used to categorize the classes and interfaces so that they can be easily
maintained.
Package provides access protection.
Package removes naming collision.
package pack;
public class A 2
{
public void msg()
{
System.out.println("Hello");
}
}
package mypack;
import pack.*;
class B{
public static void main(String args[])
{
A obj = new A();
obj.msg();
}
}
3
Use of this keyword
student(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student e1 = new Student(111,"karan");
Student e2 = new Student(222,"Aryan");
e1.display();
e2.display();
}
}
this() can be used to invoked current class constructor.
7
The this() constructor call can be used to invoke the current class constructor
(constructor chaining). This approach is better if you have many constructors in the
class and want to reuse that constructor.
class Student{
int id;
String name;
Student (){System.out.println("default constructor is invoked");}
Student(int id,String name){
this ();//it is used to invoked current class constructor.
this.id = id;
this.name = name;
}
void display() { System.out.println(id+" "+name); }
The this keyword can also be passed as an argument in the method. It is mainly used in the event
handling.
class S{
void m(S obj)
{
System.out.println("method is invoked");
}
void p()
{
m(this);
}
public static void main(String args[]){
S s1 = new S();
s1.p();
}
}
11
Inheritance
Inheritance is a mechanism in which one object acquires all the properties and behaviours of
parent object.
Inheritance enables a class to:
Inherit data members and methods from another class.
Reuse the functionalities and capabilities of the existing class by extending a new class from the
existing class and adding new features to it.
The class that inherits the data members and methods from another class is known as the
subclass.
The class from which the subclass inherits is known as the superclass.
The superclass is also referred to as the base class, and the subclass is referred to as the derived
class.
You can create additional data members and methods to add more features in a subclass.
A superclass can also be a subclass of another class.
12
Implementing Different Types of
Inheritance
Single level inheritance
Derives a subclass from a single superclass. For example, subclasses B and C inherit the
properties of a single superclass, A. The following figure shows the structure of single
level inheritance:
13
Method overriding is defined as creating a method in the subclass that has the same return
type and signature as a method defined in the super class. Signature of a method includes
the name, number, sequence, and type of arguments of a method.
The created method of the subclass hides the method defined in the superclass.
Method overriding implements the concept of polymorphism. It enables to create objects that
respond to the same method as defined in the superclass.
The inherited method should have different behavior in the subclass. When an object calls a
method, the Java compiler first searches for the method in the class of that object. If the
method is not found, then the compiler searches for the method in the class hierarchy until it is
found.
interface <interfacename>
{ //interface body
static final data members
return type public methods(parameters);
}
25
class Products
{
public static void main(String args[])
{
SoftwareProducts s = new SoftwareProducts();
HardwareProducts h = new HardwareProducts();
s.calculate();
h.calculate1();
s.noOfItems();
h.noOfItems();
}
}
interface Printable 29
{
void print();
}
An abstract class is declared with abstract access specifier and it may or may not
include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed. For example:
Shape
An abstract class defines the common properties and behaviors of other classes.
It is used as a base class to derive specific classes of the same type. For example:
abstract class Shape
{
public abstract float calculateArea();
}
The preceding abstract method, calculateArea, is inherited by the subclasses of the Shape
class. The subclasses Rectangle, Circle, and Hexagon implement this method in different
ways.
32
The super is a reference variable that is used to refer immediate parent class object.
Whenever we create the instance of subclass, an instance of parent class is created
implicitly i.e. referred by super reference variable.
class Vehicle{
int speed=50;
}
void display(){
System.out.println(speed);
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
36
class Vehicle{
Vehicle(){
System.out.println("Vehicle is created");
}
}
class Bike extends Vehicle{
Bike(){
super();
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
37
super can be used to invoke parent class method
class Person{
void message()
{
System.out.println("welcome");}
}
class Student extends Person{
void message(){
System.out.println("welcome to java");
}
void display(){
message();
super.message();
}
public static void main(String args[]){
Student s=new Student();
s.display();
}
}