100% found this document useful (1 vote)
315 views30 pages

Java Programming Micro Project (Diploma)

This document provides information about a micro project completed by four students on the topics of inheritance, interfaces, and Java packages. It includes a title page with the project title, subject, guide, and academic year. It also includes a certificate, the group member names, an acknowledgement, and an index which outlines the contents of the project report. The report contents include introductions to inheritance, interfaces, and examples of different types of inheritance in Java code.

Uploaded by

Ninad Dalvi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (1 vote)
315 views30 pages

Java Programming Micro Project (Diploma)

This document provides information about a micro project completed by four students on the topics of inheritance, interfaces, and Java packages. It includes a title page with the project title, subject, guide, and academic year. It also includes a certificate, the group member names, an acknowledgement, and an index which outlines the contents of the project report. The report contents include introductions to inheritance, interfaces, and examples of different types of inheritance in Java code.

Uploaded by

Ninad Dalvi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 30

G.V.

ACHARYA POLYTECHNIC
VEERACHARYA TECHNICAL EDUCATION CAMPUS,OPP.SHELU SUBURBAN RAILWAY STATION,

SHELU/DAMAT, TAL.KARJAT, DIST.RAIGAD, PIN-410 201

Micro Project Title

Inheritance, Interfaces, & Java Packages

The Subject

Java Programming

Guide By :-

Pratul Karande

Acadamic Year

2020-2021

Maharashtra State Broard Of Technical Education

1
MAHARASHTRA BOARD OF TECHNICAL EDUCATIONCERTIFICATE

This is certify that___________________________________________________

Of Semester-IV of Diploma in COMPUTER ENGINEERING of institute G.V.


ACHARYAPOLYTECHNIC , Shelu has completed the Micro Project satisfactory in
subject JAVA PROGRAMMING,foracdemic year 2020-21 as prescribed in the
curriculum.

PLACE:

DATE:___________________

SUBJECT H.O.D. PRINCIPAL

2
GROUP NAMES

ENROLLMENT NO. NAMES


1911440026 KOMAL TAWARI
1911440031 SIDDHI MANORE
1911440040 NINAD DALVI
1911440047 RESHMA KHARMARE

3
ACKNOWLEDGEMENT

Success is nourished under the combination of perfect guidance, care and blessings.

Acknowledgement is the best way to convey. We express deep sense of gratitude and
brightness to the outstanding permutations with success. This year we have to spend
this estimated instituion has moulded us in to confident and aspiring Engineers.

We express our sense of gratitude towards our projcts guide,

MR.PRATUL KARANDEIt is because of his valuable guidance, analytical approach and


encouragement that we could learn, work and comlete the project. We will always
cherish the great experience to work under their enthusiastic guidance.

We are aslogreatful to our vice-principal MRS.RUPALIMA'AMwho not only supported


us in our project but also encouraged us for every creative activity.

MR.MITHUN JADHAVHead of Department who gave us the opportunity to carry out


our project work.

We extend our special thanks to all teaching and non-teaching staff, friends and
wellwshiers who directly or indirectly contribute for the success of our maiden
missions. Finally, how can we forget our parents whose loving support and faith in us
remains our prime source of inspiration.

Lastly I would like to thanks to all those who directly and indirectly helped In completion
of this project. I would aslo like to acknowledge and appreciate the crucial role of staff
who gave permission tO use all reqired and necessary material to complete this project.

4
INDEX

1. INTRODUCTION
2. CONTENT
3. REFERENCE
4. CONCLUSION
5. ANNEXURE

5
ABSTRACT

Java is a high level, object-oriented, platform independent


language.

Java, unlike some languages before it allows for the use of


words and commands instead of just symbols and numbers.
Java also allows for the creation of advanced data types called
objects which represent real world things like a chair or a
computer where you can set the attributes of these objects and
things they do.

Java is very flexible - it can be used to develop software as well


as applets (small programs that run on webpages). But the
flexibility doesn't end there because you can run the same Java
programs on various operating systems without having to
rewrite the code (unlike some other languages such as C and
C++) thanks to the Java run-time environment which interprets
Java code and tells the operating system what to do.

6
Java – Inheritance

Inheritance in Java is a mechanism in which one object acquires all the


properties and behaviors of a parent object. It is an important part
of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class,
you can reuse methods and fields of the parent class. Moreover, you can
add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as


a parent-child relationship.

Why use inheritance in java

o For Method Overriding (so runtime polymorphism can be achieved).


o For Code Reusability.

7
Terms used in Inheritance

o Class: A class is a group of objects which have common properties.


It is a template or blueprint from which objects are created.
o Sub Class/Child Class: Subclass is a class which inherits the other
class. It is also called a derived class, extended class, or child class.
o Super Class/Parent Class: Superclass is the class from where a
subclass inherits the features. It is also called a base class or a
parent class.
o Reusability: As the name specifies, reusability is a mechanism which
facilitates you to reuse the fields and methods of the existing class
when you create a new class. You can use the same fields and
methods already defined in the previous class.

The syntax of Java Inheritance

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.

In the terminology of Java, a class which is inherited is called a parent or


superclass, and the new class is called child or subclass

8
Java Inheritance Example

As displayed in the above figure, Programmer is the subclass and


Employee is the superclass. The relationship between the two classes
is Programmer IS-A Employee. It means that Programmer is a type of
Employee.

In the above example, Programmer object can access the field of own
class as well as of Employee class i.e. code reusability

9
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java:
single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through

interface only. We will learn about interfaces later.

When one class inherits multiple classes, it is known as multiple


inheritance. For Example:

10
Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In
the example given below, Dog class inherits the Animal class, so there is
the single inheritance.

Output:

11
Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance.
As you can see in the example given below, BabyDog class inherits the
Dog class which again inherits the Animal class, so there is a multilevel
inheritance.

Output:

12
Hierarchical Inheritance Example
When two or more classes inherits a single class, it is known
as hierarchical inheritance. In the example given below, Dog and Cat
classes inherits the Animal class, so there is hierarchical inheritance.

Output:

13
Java - Interfaces
An interface is a reference type in Java. It is similar to class. It is a
collection of abstract methods. A class implements an interface, thereby
inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants,
default methods, static methods, and nested types. Method bodies exist
only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the
attributes and behaviors of an object. And an interface contains behaviors
that a class implements.
Unless the class that implements the interface is abstract, all the methods
of the interface need to be defined in the class.

An interface is similar to a class in the following ways −


 An interface can contain any number of methods.
 An interface is written in a file with a .java extension, with the name
of the interface matching the name of the file.
 The byte code of an interface appears in a .class file.
 Interfaces appear in packages, and their corresponding bytecode file
must be in a directory structure that matches the package name.

However, an interface is different from a class in several ways, including −


 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can
appear in an interface must be declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
 An interface can extend multiple interfaces.

14
Declaring Interfaces
The interface keyword is used to declare an interface. Here is a simple
example to declare an interface −
Example
Following is an example of an interface −

/* File name : NameOfInterface.java */


importjava.lang.*;
// Any number of import statements

publicinterfaceNameOfInterface{
// Any number of final, static fields
// Any number of abstract method declarations\
}

Interfaces have the following properties −


 An interface is implicitly abstract. You do not need to use
the abstract keyword while declaring an interface.
 Each method in an interface is also implicitly abstract, so the abstract
keyword is not needed.
 Methods in an interface are implicitly public.
Example

/* File name : Animal.java */


interfaceAnimal{
publicvoid eat();
publicvoid travel();
}

15
Implementing Interfaces
When a class implements an interface, you can think of the class as
signing a contract, agreeing to perform the specific behaviors of the
interface. If a class does not perform all the behaviors of the interface, the
class must declare itself as abstract.
A class uses the implements keyword to implement an interface. The
implements keyword appears in the class declaration following the extends
portion of the declaration.
Example

/* File name : MammalInt.java */


publicclassMammalIntimplementsAnimal{

publicvoid eat(){
System.out.println("Mammal eats");
}

publicvoid travel(){
System.out.println("Mammal travels");
}

publicintnoOfLegs(){
return0;
}

publicstaticvoid main(Stringargs[]){
MammalInt m =newMammalInt();
m.eat();
m.travel();
}
}

This will produce the following result −

Output
Mammal eats
Mammal travels

16
When overriding methods defined in interfaces, there are several rules to
be followed −
 Checked exceptions should not be declared on implementation
methods other than the ones declared by the interface method or
subclasses of those declared by the interface method.
 The signature of the interface method and the same return type or
subtype should be maintained when overriding the methods.
 An implementation class itself can be abstract and if so, interface
methods need not be implemented.

When implementation interfaces, there are several rules −


 A class can implement more than one interface at a time.
 A class can extend only one class, but implement many interfaces.
 An interface can extend another interface, in a similar way as a class
can extend another class.

17
Extending Interfaces
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.
The following Sports interface is extended by Hockey and Football
interfaces.
Example

// Filename: Sports.java
publicinterfaceSports{
publicvoidsetHomeTeam(String name);
publicvoidsetVisitingTeam(String name);
}

// Filename: Football.java
publicinterfaceFootballextendsSports{
publicvoidhomeTeamScored(int points);
publicvoidvisitingTeamScored(int points);
publicvoidendOfQuarter(int quarter);
}

// Filename: Hockey.java
publicinterfaceHockeyextendsSports{
publicvoidhomeGoalScored();
publicvoidvisitingGoalScored();
publicvoidendOfPeriod(int period);
publicvoidovertimePeriod(intot);
}

The Hockey interface has four methods, but it inherits two from Sports;
thus, a class that implements Hockey needs to implement all six methods.
Similarly, a class that implements Football needs to define the three
methods from Football and the two methods from Sports.

18
Extending Multiple Interfaces

A Java class can only extend one parent class. Multiple inheritance is not
allowed. Interfaces are not classes, however, and an interface can extend
more than one parent interface.
The extends keyword is used once, and the parent interfaces are declared
in a comma-separated list.
For example, if the Hockey interface extended both Sports and Event, it
would be declared as −
Example

publicinterfaceHockeyextendsSports,Event

Tagging Interfaces

The most common use of extending interfaces occurs when the parent
interface does not contain any methods. For example, the MouseListener
interface in the java.awt.event package extended java.util.EventListener,
which is defined as −

Example

packagejava.util;
publicinterfaceEventListener
{}

19
An interface with no methods in it is referred to as a tagging interface.
There are two basic design purposes of tagging interfaces −

Creates a common parent − As with the EventListener interface, which is


extended by dozens of other interfaces in the Java API, you can use a
tagging interface to create a common parent among a group of interfaces.
For example, when an interface extends EventListener, the JVM knows
that this particular interface is going to be used in an event delegation
scenario.

Adds a data type to a class − This situation is where the term, tagging
comes from. A class that implements a tagging interface does not need to
define any methods (since the interface does not have any), but the class
becomes an interface type through polymorphism.

20
Java - Packages

Packages are used in Java in order to prevent naming conflicts, to control


access, to make searching/locating and usage of classes, interfaces,
enumerations and annotations easier, etc.

A Package can be defined as a grouping of related types (classes,


interfaces, enumerations and annotations ) providing access protection
and namespace management.

Some of the existing packages in Java are −


 java.lang − bundles the fundamental classes
 java.io − classes for input , output functions are bundled in this
package

Programmers can define their own packages to bundle group of


classes/interfaces, etc. It is a good practice to group related classes
implemented by you so that a programmer can easily determine that the
classes, interfaces, enumerations, and annotations are related.

Since the package creates a new namespace there won't be any name
conflicts with names in other packages. Using packages, it is easier to
provide access control and it is also easier to locate the related classes.

21
Creating a Package

While creating a package, you should choose a name for the package and
include a package statement along with that name at the top of every
source file that contains the classes, interfaces, enumerations, and
annotation types that you want to include in the package.
The package statement should be the first line in the source file. There can
be only one package statement in each source file, and it applies to all
types in the file.
If a package statement is not used then the class, interfaces,
enumerations, and annotation types will be placed in the current default
package.
To compile the Java programs with package statements, you have to use
-d option as shown below.
javac -d Destination_folderfile_name.java
Then a folder with the given package name is created in the specified
destination, and the compiled class files will be placed in that folder.

Example

Let us look at an example that creates a package called animals. It is a


good practice to use names of packages with lower case letters to avoid
any conflicts with the names of classes and interfaces.

Following package example contains interface named animals −


/* File name : Animal.java */
package animals;

interfaceAnimal{
publicvoid eat();
publicvoid travel();
}

22
Now, let us implement the above interface in the same package animals –
package animals;
/* File name : MammalInt.java */

publicclassMammalIntimplementsAnimal{

publicvoid eat(){
System.out.println("Mammal eats");
}

publicvoid travel(){
System.out.println("Mammal travels");
}

publicintnoOfLegs(){
return0;
}

publicstaticvoid main(Stringargs[]){
MammalInt m =newMammalInt();
m.eat();
m.travel();
}
}
Now compile the java files as shown below −
$ javac -d . Animal.java
$ javac -d . MammalInt.java
Now a package/folder with the name animals will be created in the current
directory and these class files will be placed in it as shown below.

23
You can execute the class file within the package and get the result as
shown below.
Mammal eats
Mammal travels

The import Keyword


If a class wants to use another class in the same package, the package
name need not be used. Classes in the same package find each other
without any special syntax.

Example
Here, a class named Boss is added to the payroll package that already
contains Employee. The Boss can then refer to the Employee class
without using the payroll prefix, as demonstrated by the following Boss
class.

package payroll;
publicclassBoss{
publicvoidpayEmployee(Employee e){
e.mailCheck();
}
}

What happens if the Employee class is not in the payroll package? The
Boss class must then use one of the following techniques for referring to a
class in a different package.

 The fully qualified name of the class can be used. For example –
payroll.Employee
 The package can be imported using the import keyword and the wild
card (*). For example –
import payroll.*;

24
 The class itself can be imported using the import keyword. For
example −
importpayroll.Employee;

Note − A class file can contain any number of import statements. The
import statements must appear after the package statement and before
the class declaration.

The Directory Structure of Packages


Two major results occur when a class is placed in a package −
 The name of the package becomes a part of the name of the class,
as we just discussed in the previous section.
 The name of the package must match the directory structure where
the corresponding bytecode resides.

25
Here is simple way of managing your files in Java −
Put the source code for a class, interface, enumeration, or annotation type
in a text file whose name is the simple name of the type and whose
extension is .java.
For example −
// File Name : Car.java
package vehicle;

publicclassCar{
// Class implementation.
}
Now, put the source file in a directory whose name reflects the name of
the package to which the class belongs −
....\vehicle\Car.java
Now, the qualified class name and pathname would be as follows −

 Class name → vehicle.Car


 Path name → vehicle\Car.java (in windows)
In general, a company uses its reversed Internet domain name for its
package names.
Example − A company's Internet domain name is apple.com, then all its
package names would start with com.apple. Each component of the
package name corresponds to a subdirectory.
Example − The company had a com.apple.computers package that
contained a Dell.java source file, it would be contained in a series of
subdirectories like this −
....\com\apple\computers\Dell.java
At the time of compilation, the compiler creates a different output file for
each class, interface and enumeration defined in it. The base name of the
output file is the name of the type, and its extension is .class.

For example −

26
// File Name: Dell.java
packagecom.apple.computers;

publicclassDell{
}

classUps{
}

Now, compile this file as follows using -d option −


$javac -d . Dell.java
The files will be compiled as follows −
.\com\apple\computers\Dell.class
.\com\apple\computers\Ups.class
You can import all the classes or interfaces defined
in \com\apple\computers\ as follows −
import com.apple.computers.*;
Like the .java source files, the compiled .class files should be in a series of
directories that reflect the package name. However, the path to the .class
files does not have to be the same as the path to the .java source files.
You can arrange your source and class directories separately, as −
<path-one>\sources\com\apple\computers\Dell.java

<path-two>\classes\com\apple\computers\Dell.class
By doing this, it is possible to give access to the classes directory to other
programmers without revealing your sources. You also need to manage
source and class files in this manner so that the compiler and the Java
Virtual Machine (JVM) can find all the types your program uses.
The full path to the classes directory, <path-two>\classes, is called the
class path, and is set with the CLASSPATH system variable. Both the
compiler and the JVM construct the path to your .class files by adding the
package name to the class path.

27
Say <path-two>\classes is the class path, and the package name is
com.apple.computers, then the compiler and JVM will look for .class files
in <path-two>\classes\com\apple\computers.
A class path may include several paths. Multiple paths should be
separated by a semicolon (Windows) or colon (Unix). By default, the
compiler and the JVM search the current directory and the JAR file
containing the Java platform classes so that these directories are
automatically in the class path.

Set CLASSPATH System Variable


To display the current CLASSPATH variable, use the following commands
in Windows and UNIX (Bourne shell) −

 In Windows → C:\> set CLASSPATH


 In UNIX → % echo $CLASSPATH
To delete the current contents of the CLASSPATH variable, use −

 In Windows → C:\> set CLASSPATH =


 In UNIX → % unset CLASSPATH; export CLASSPATH
To set the CLASSPATH variable −

 In Windows → set CLASSPATH = C:\users\jack\java\classes


 In UNIX → % CLASSPATH = /home/jack/java/classes; export
CLASSPATH

28
REFERENCE
WWW.JAVAPOINT.COM

WWW.TUTORIALSPOINT.COM

WWW.W3SCHOOLS.ORG

WWW.PROGRAMIZ.COM

29
ANNEXURE

Acadamic Year: Name of Faculty:

Course: Course code: Semester:

Title of project: …………………………………………………………………………………..............................

Cos addressed by the Micro Project: ……………………………………………………………………….......

A: …………………………………………………………………………………………………………………………………

B: …………………………………………………………………………………………………………………………………

C: …………………………………………………………………………………………………………………………………

D: …………………………………………………………………………………………………………………………………

Major Learning Outcomes achieved by students by doing the projects:

a)Practicle Outcomes:

.......................................................................................................................
.......................................................................................................................
.......................................................................................................................

Comments/suggestions about team, work/ leadership/ inter-personal


communications (if any)

.......................................................................................................................
.
.......................................................................................................................
.
...............................................................................................................

30

You might also like