0% found this document useful (0 votes)
20 views

07 Java Fundamental Package, Interface

Uploaded by

amitchaurasia774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

07 Java Fundamental Package, Interface

Uploaded by

amitchaurasia774
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

1

Package in Java

A package is a group of similar types of classes, interfaces and sub-packages.

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

In java, this is a reference variable that refers to the current object.

Usage of this keyword


this keyword can be used to refer current class instance variable.
this can be used to invoke current class constructor.
this keyword can be used to invoke current class method (implicitly)
this can be passed as an argument in the method call.
this can be passed as argument in the constructor call.
this keyword can also be used to return the current class instance.
Code without using this keyword 4
class student
{
int id;
String name;
student(int id,String name)
{
id = id;
name = name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
student s1 = new student(111,"Karan");
student s2 = new student(321,"Aryan");
s1.display();
s2.display();
}
}
Code with using this keyword 5
class Student{
int id;
String name;

student(int id,String name){


this.id = id;
this.name = name;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
}
}
Program where this keyword is not required
6
class Student{
int id;
String name;

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); }

public static void main(String args[]){


Student e1 = new Student(111,"karan");
Student e2 = new Student(222,"Aryan");
e1.display();
e2.display();
}
}
class Student{
int id; 8
String name;
String city;

Student(int id,String name){


this.id = id;
this.name = name;
}
Student(int id,String name,String city){
this(id,name);//now no need to initialize id and name
this.city=city;
}
void display(){System.out.println(id+" "+name+" "+city);}

public static void main(String args[]){


Student e1 = new Student(111,"karan");
Student e2 = new Student(222,"Aryan","delhi");
e1.display();
e2.display();
}
}
class Student{ 9
int id;
String name;
Student () {
System.out.println("default constructor is invoked");
}

Student(int id,String name){


id = id;
name = name;
this ();//must be the first statement
}
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 keyword can be passed as an 10
argument in the method.

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

The following syntax shows how to implement single level inheritance:


class A
{
}
class B extends A
{
}
class C extends A
{
}
In the preceding syntax, the extends keyword is used to derive a
subclass from a superclass.
class Book //Declaration of the Superclass 14
{
//Declaring the data members.
String author= "Steve";
String title = "Handbook";
int price = 50;
int pages = 350;
int stock = 13;
//Defining the methods.
public void show()
{
System.out.println(" ");
System.out.println("\t Books Information");
System.out.println("\t Book Author: " + author);
System.out.println("\t Book Title: " + title);
System.out.println("\t Book Price: " + price);
System.out.println("\t Number of pages: " + pages);
System.out.println("\t Book Stock: " + stock);
System.out.println(" ");
}
}
15
class HardwareBook extends Book//Declaration of the Subclass
{
//Declaring the data members.
String hardwareTitle = "Printers";
String publisher = "Tom Wilkins";
//Defining the method, showData()
public void showData()
{
show();
System.out.println(" ");
System.out.println("\t Hardware Books Information");
System.out.println("\t Hardware Title: " + hardwareTitle);
System.out.println("\t Publisher Name: " + publisher);
System.out.println(" ");
}
}
class SoftwareBook extends Book//Declaration of the Subclass
{
16
//Declaring the data members.
String softwareName = "Windows";
String softwareVersion = "Mary Peterson";
//Definition of method
public void showDetails()
{
show();//Calling the method of Book class.
System.out.println("\t Software Books Information");
System.out.println("\t Software Name: " + softwareName);
System.out.println("\t Software Version: " +softwareVersion);
System.out.println(" ");
}
public static void main(String args[])
{
HardwareBook h = new HardwareBook();
SoftwareBook s = new SoftwareBook();
s.showDetails();
h.showData();
}
}
17
Multilevel inheritance

Inherits the properties of another subclass.


For example,
Class A is a superclass for the Class B; and Class B is a superclass for
the subclass, Class C. You can include any number of levels in
multilevel inheritance. The following figure shows the structure of
multilevel inheritance:
class Book 18
{
int price;
int pages;
public void get(int mprice, int mpages)
{
price = mprice;
pages = mpages;
}
public void show()
{
System.out.println(" ");
System.out.println("\t Books Information");
System.out.println("\t Book Price: " + price);
System.out.println("\t Number of pages: " + pages);
System.out.println(" ");
}
}
19
class SoftwareBook extends Book
{
String softwareName;
String softwareVersion;
public void getDetails(String msoftwareName, String msoftwareVersion)
{
softwareName = msoftwareName;
softwareVersion = msoftwareVersion;
}
public void showDetails()
{
System.out.println(" ");
System.out.println("\t Software Books Information");
System.out.println("\t Software Name: " + softwareName);
System.out.println("\t Software Version: " +
softwareVersion);
System.out.println(" ");
}
}
class Cplus extends SoftwareBook {
String author; 20
String title;
public void getData(String mauthor, String mtitle) {
author = mauthor;
title = mtitle; }
public void showData() {
show();
showDetails();
System.out.println(" ");
System.out.println("\t C++ Books Information");
System.out.println("\t Author Name: " + author);
System.out.println("\t Book Title: " + title);
System.out.println(" ");
} public static void main(String args[]) {
Cplus c = new Cplus();
c.get(45, 450);
c.getDetails("Borland C++", "5.0");
c.getData("Lee Mitchell", "Programming using C++");
c.showData();
}
}
21
Method overriding

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.

WE cannot override the static and final methods of a superclass


class Book {
int price = 50; 22
int pages = 500;
public void show() {
System.out.println(" ");
System.out.println("\t Books Information");
System.out.println("\t Book Price: " + price);
System.out.println("\t Number of pages: " + pages);
System.out.println(" ");
} }
class SoftwareBook extends Book {
String softwareName = "Borland C++";
String softwareVersion = "5.0";
public void show()
{
System.out.println(" ");
System.out.println("\t Software Books Information");
System.out.println("\t Software Name: " + softwareName);
System.out.println("\t Software Version: " +
softwareVersion);
System.out.println(" ");
}
23

public static void main(String args[])


{
SoftwareBook s = new SoftwareBook();
s.show();
}
}
24
Implementing Interfaces

• Interfaces contain a set of abstract methods and static data members.


• Interface is known as a prototype for a class.
• Methods defined in an interface are only abstract methods.
• An abstract method contains only the declaration for a method without any
implementation details.
• The implementation of an abstract method is defined in the class implementing
the interface.
• You can implement multiple interfaces in a single class.

interface <interfacename>
{ //interface body
static final data members
return type public methods(parameters);
}
25

•The following syntax shows how to implement an interface in a class:


class <class_name> extends [superclass] implements [interfacename]
{
//Defining the method declared in the interface.
return type public methods(parameters)
{
}
}
•In the preceding syntax, a class that extends from a superclass implements an interface
using the implements keyword.
interface ItemCount //Defining the interface.
{ 26
//Defining abstract methods in the Billing
interface.
public void noOfItems();
}
class SoftwareProducts implements ItemCount
{
int os= 60;
int oracle = 20;
int jav = 15;
int tot;
public void calculate(){
tot = os + oracle + jav;
}
public void noOfItems()
{
System.out.println();
System.out.println(" "+"Total number of software products
purchased is :"+ tot);}
}
27
class HardwareProducts implements ItemCount
{
int printer = 20;
int speaker = 12;
int harddisk = 25;
int htot;
public void calculate1()
{
htot = printer + speaker + harddisk;
}
public void noOfItems()
{
System.out.println();
System.out.println(" "+"Total number of hardware items purchased
is : “ +htot);
}
}
28

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();
}

interface Showable extends Printable


{
void show();
}
class A implements Showable{

public void print(){System.out.println("Hello");}


public void show(){System.out.println("Welcome");}

public static void main(String args[])


{
A obj = new A();
obj.print();
obj.show();
}
}
30
Abstract Classes

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

Circle Rectangle Hexagon


31

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

A simple example of implementation of Abstract Method:


public class Circle extends Shape
{
float radius;
public float calculateArea()
{
return ((radius * radius)* (22/7));
}
}
In the preceding example, the calculateArea() method has been overridden in the Circle class.
33

abstract class Bike


{
abstract void run();
}
class Honda extends Bike
{
void run()
{
System.out.println("running safely..");
}

public static void main(String args[])


{
Bike obj = new Honda();
obj.run();
} }
34
super keyword

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.

Usage of super Keyword


super is used to refer immediate parent class instance variable.
super() is used to invoke immediate parent class constructor.
super is used to invoke immediate parent class method.
35

class Vehicle{
int speed=50;
}

class Bike extends Vehicle{


int speed=100;

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();
}
}

You might also like