OOP CS3391 UNIT-1[Lecture Note-8]
OOP CS3391 UNIT-1[Lecture Note-8]
Of the above Sections shown in the figure, the Main Method class is Essential part,
Documentation Section is a suggested part and all the other parts are optional.
Documentation Section
It Comprises a Set of comment lines giving the name of the program, the author and
other details.
Comments help in Maintaining the Program.
Java uses a Style of comment called documentation comment. /* * …… */
This type of comment helps in generating the documentation automatically.
Example:
/*
* Title: Conversion of Degrees
* Aim: To convert Celsius to Fahrenheit and vice versa
* Date: 10/08/2024
* Author: Justin
*/
Package Statement
The first statement allowed in a Java file is a package statement.
It declares the package name and informs the compiler that the classes defined belong
to this package.
Example :
package student;
package basepackage.subpackage.class;
It is an optional declaration.
Import Statements
The statement instructs the interpreter to load a class contained in a particular
package.
Example : import student.test;
Where, student is the package and test is the class.
Interface Statements
We can create an interface in this section if required. We use the interface keyword
to create an interface.
Example: interface car
{ }
An interface is similar to classes which consist of group of method declaration.
Like classes, interfaces contain methods and variable.
To link the interface to our program, the keyword implements is used.
Example: public class xx extends Applet implements ActionListener
where, xx – class name (subclass of Applet)
Applet – Base class name
ActionListener – interface
Extends & implements - keywords
It is used when we want to implement the feature of Multiple Inheritance in Java
It is an optional declaration.
Class Definitions
In this section, we define the class.
A Java Program can have any number of class declarations.
The number of classes depends on the complexity of the program.
Main Method Class
Every Java Standalone program requires a main method as its starting point.
A Simple Java Program will contain only the main method class.
It creates objects of various classes and uses those objects for performing various
operations.
When the end of main is reached the program terminates and the control transferred
back to the Operating system.
Syntax for writing main:
public static void main(String arg[ ])
where,
public – It is an access specifier to control the visibility of class members. main()
must be declared as public, since it must be called by code outside of its class
when the program is started.
static – this keyword allows main() method to be called without having to
instantiate the instance of the class.
void – this keyword tells the compiler that main() does not return any value.
main() – is the method called when a Java application begins.
String arg[] – arg is an string array which receives any command-line arguments
present when the program is executed.
Rules to be followed to write Java Programs:
Case Sensitivity - Java is case sensitive, which means identifier Hello and hello would
have different meaning in Java.
Class Names - For all class names the first letter should be in Upper Case.
If several words are used to form a name of the class, each inner word's first letter should
be in Upper Case.
Example:- class MyFirstJavaClass
Method Names - All method names should start with a Lower Case letter.
If several words are used to form the name of the method, then each inner word's first
letter should be in Upper Case.
Example public void myMethodName()
Program File Name - Name of the program file should exactly match the class name.When
saving the file, you should save it using the class name (Remember Java is case
sensitive) and append '.java' to the end of the name (if the file name and the class name do not
match your program will not be compiled).
Example : Assume 'MyFirstJavaProgram' is the class name. Then the file should be saved
as 'MyFirstJavaProgram.java'
public static void main(String args[]) - Java program processing starts from the main()
method which is a mandatory part of every Java program.