CH 1
CH 1
INTRODUCTION
OBJECT ORIENTED PARADIGM
• OOP is the core of JAVA. Java is pure OOP.
• Major objective of OOP is to eliminate flow of data in procedural
approach.
• In OOP, data is a critical element & it doesn’t flow freely around
the system.
• OOP allows us to decompose problem into number of entities
called objects and then build data & move around these objects.
• Object is combination of data and methods.
• Objects are real time entities in an object oriented system.
• Entire set of data & code of an object can be made a user
defined data type using concept of class.
• A class is a user defined data type & object is a variable.
• Once class is defined we can create any number of objects
belonging to class.
• Data of an object can be accessed only by methods associated
with the object.
FEATURES OF OOP
1. Data Abstraction and Encapsulation
2. Inheritance
3. Polymorphism
4. Dynamic Binding
5. Message Communication
Data Abstraction and Encapsulation
Java provides the efficient memory C does not provide the efficient memory
management. management.
Java does not support preprocessor C supports the preprocessor statement like
statement like #define. #define and #include.
Java supports the exception handling to C does not support exception handling to
handle the exception at run time. handle the errors.
Difference between JAVA & C++
Java C++
Java is a true and complete object oriented C++ is an extension of C with object
language oriented behavior. C++ is not a complete
object oriented language as that of Java.
• import packageName;
public class ClassName
{
public static void main (String args[])
{
// Your logic Goes Here
}
}
First Java Program
class JavaDemo
{
public static void main(String args[])
{
System.out.println(“Hello World”);
}
}
First Java Program
• Class declaration:
As Java is pure object – oriented, everything must be placed inside class.
here “class” is the keyword that declares new class definition.
“JavaDemo” is the identifier that specifies the name of the class.
For most computer languages, the name of the file that holds the source
code to a program is arbitrary. But this is not the case with Java.
For this example, the name of the source file should be JavaDemo.java.
In Java, a source file is officially called a compilation unit.
It is a text file that contains one or more class definitions.
The Java compiler requires that a source file use the .java filename
extension.
The name of that class should match the name of the file that holds the
program.
You should also make sure that the capitalization of the filename matches
the class name.
The reason for this is that Java is case-sensitive.
First Java Program
The javac compiler creates a file called JavaDemo.class that contains
the bytecode version of the program.
When Java source code is compiled, each individual class is put into
its own output file named after the class and using the .class
extension.
This is why give your Java source files the same name as the class
they contain—the name of the source file will match the name of
the .class file.
“java JavaDemo” is used to run the code.
When you execute the Java interpreter, you are actually specifying
the name of the class that you want the interpreter to execute.
It will automatically search for a file by that name that has the .class
extension. If it finds the file, it will execute the code contained in the
specified class.
• Opening brace :
Every class definition starts with ‘{‘ and ends with ‘}’.
Signature of main()
• public:
– It is a keyword is an access modifier which represents visibility, it
means it is visible to all.
– Since main method is public in java, JVM can easily access and
execute it.
• static:
– The main method is executed by the JVM, so it doesn’t require to
create object to invoke the main method. So it saves memory.
– The keyword static allows main( ) to be called without having to
instantiate a particular instance of the class.
– This is necessary since main( ) is called by the Java interpreter
before any objects are made.
• void:
– It is a keyword and denotes that the main() method does not return
a value.
• main():
– It is the name of the method.
Signature of main()
• String args[]:
– It declares a parameter named args, which contains an array
of object of the type String class. This is used to store
command line arguments, which are passed by user at
runtime.
• System.out.println (“ Hello World”);
– As java is true object oriented language, every method must
be a part of an object.
– The println() and print() methods are member of ‘out’ object,
which is a static data member of System class.
– println() always appends newline character to the end of the
string.
– ‘out’ displays the output on the command line.
– That means any other output will appear on the next line.
– Every java statement must end with a ‘;’.
Signature of main()
• main() is the method called when a Java application
begins.
• Keep in mind that Java is case-sensitive.
• Thus, Main is different from main.
• It is important to understand that the Java compiler
will compile classes that do not contain a main( )
method.
• But the Java interpreter has no way to run these
classes.
• So, if you had typed Main instead of main, the compiler
would still compile your program.
• However, the Java interpreter would report an error
because it would be unable to find the main( ) method.