Java Mod1 Part1
Java Mod1 Part1
Programming (OOP )
• OOP is an approach to program organization
and development, which attempts to eliminate
some of the pitfalls of conventional
programming methods by incorporating the
best of structured programming features with
several new concepts.
• Languages that support OOP features include
Smalltalk, C++, Ada and Pascal.
• C++ is basically a procedural language with
object-oriented extension.
• Java is a pure object-oriented language.
Features of Object-Oriented Paradigm
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as Objects.
• Data structures are designed such that they characterize the
objects.
• Methods that operate on data of an object are tied together
in the data structure.
• Data is hidden and cannot be accessed by external functions.
• Objects may communicate with each other through methods.
• New data and methods can be easily added whenever
necessary.
• Follow bottom-up approach in program design.
Basic Concepts of OOP
• Objects and Classes
Objects are basic runtime entities in an OOP and
contains data & code to manipulate the data.
Eg : a person, a place, a bank a/c etc.
Any programming problem is analyzed in terms of
objects and the nature of communication between
them.
An object takes up space in the memory and has an
associated memory address.
A class is a user-defined data
A class is a collection of objects of similar type.
Eg : mango, apple and Orange are members of fruit.
• Data Abstraction & Encapsulation
The wrapping up of data and methods into a single
unit (called class) is known as encapsulation.
Data is not accessible to the outside world and only
those methods, which are wrapped in the class, can
access it.
These methods provide the interface between
object’s data and the program.
This insulation of the data from direct access by the
program is called Data Hiding.
Encapsulation makes it possible for objects to be
treated like ‘Black Boxes’.
Abstraction refers to the act of representing essential
features without including the background details.
class Example
{
// your pgm begins here
public static void main(String args[])
{
System.out.println(“Welcome to Java pgm”);
}
}
The filename should be “Example.java”
Compiling the program
C:\>javac Example.java
Compiler creates a file called Example.class
contains bytecode version.
To run pgm
C:\> java Example
The following o/p is displayed
Welcome to Java pgm
• Pgm execution begins with main()
• The public keyword is access specifier, which
allows the programer to control the visibility
of class members.
• public – members are accessed by code
outside the class in which it is declared.
( accessible to all other classes)
• private – which prevents a member from
being used by code defined outside of its
class.
• static allows main() to be called without
having to instantiate a particular instance of
the class. This is necessary since main() is
called by JVM before any objects are made.
• void tells compiler that main() does not return
a value.
• String args[] – declares a parameter named
args, which contains an array of objects of the
class type String.
• Every java statement end with a semicolon.
javac Test.java
If everything OK, javac compiler creates a file called Test.class containing the
bytecode of the program.
• Range is 0 – 65,536
• The subsequent use of these name in the program has the
effect of caving their defined values to be automatically
substituted in appropriate points. The constant is declared
as follows:
Rules :-
• symbolic names take the some form as variable names. But they one
written in capitals to distinguish from variable names. This is only
convention not a rule.
• They can’t be declared inside a method . they should be used only as
class data members in the beginning of the class.
Type Casting
Getting Values of Variables
Standard Default Values