Java Programming Micro Project (Diploma)
Java Programming Micro Project (Diploma)
ACHARYA POLYTECHNIC
VEERACHARYA TECHNICAL EDUCATION CAMPUS,OPP.SHELU SUBURBAN RAILWAY STATION,
The Subject
Java Programming
Guide By :-
Pratul Karande
Acadamic Year
2020-2021
1
MAHARASHTRA BOARD OF TECHNICAL EDUCATIONCERTIFICATE
PLACE:
DATE:___________________
2
GROUP NAMES
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 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
6
Java – Inheritance
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.
7
Terms used in Inheritance
8
Java Inheritance Example
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.
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.
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 −
publicinterfaceNameOfInterface{
// Any number of final, static fields
// Any number of abstract method declarations\
}
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
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();
}
}
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.
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 −
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
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
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
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.
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 −
For example −
26
// File Name: Dell.java
packagecom.apple.computers;
publicclassDell{
}
classUps{
}
<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.
28
REFERENCE
WWW.JAVAPOINT.COM
WWW.TUTORIALSPOINT.COM
WWW.W3SCHOOLS.ORG
WWW.PROGRAMIZ.COM
29
ANNEXURE
A: …………………………………………………………………………………………………………………………………
B: …………………………………………………………………………………………………………………………………
C: …………………………………………………………………………………………………………………………………
D: …………………………………………………………………………………………………………………………………
a)Practicle Outcomes:
.......................................................................................................................
.......................................................................................................................
.......................................................................................................................
.......................................................................................................................
.
.......................................................................................................................
.
...............................................................................................................
30