Professional Documents
Culture Documents
Object Oriented
Object Oriented
INTRODUCTION TO JAVA
Java is a general purpose object oriented programming language developed by
Sun Micro system of USA in 1991, originally called OAK.A team of Sun Micro
system programmers headed by James Gosling was formed to undertake this
task.
Some of the features of Java developed by Sun Micro Systems are:
• Compiled & Interpreted
• Platform independent & portable
• Object Oriented
• Robust & Secure
• Distributed
• Dynamic & Extensible
• Compiled & Interpreted: Usually a computer language is either compiled or
interpreted but java combines both these approaches thus making java a
two stage system. First java compiler translates source code into what is
known as byte code instruction, but these are not machine instructions and
therefore in the second stage java interpreter generates machine code that
can be directing executed by the machine that is running the java program.
• Platform independent & Portable: The most significance contribution of
java over other languages is its portability. Java programs can be easily
moved from one computer to another anywhere and anytime. Changes and
upgrades in operating system, processors and system resources will not
force any changes in java program. This is the reason why java has became
a popular language for programming on internal where different user using
different kinds of computer system.
So we can run java programs on a wide variety of computer system using a
range of computer system. Java programs will run just as well on a PC running
Microsoft windows as it will on Linux or a Sun Solaris workstation. This is possible
because a java programs does not execute directly on our computer. It runs on a
standardized hypothetical computer that is called a java virtual machine or (JVM)
which is executed inside our computer by a program.
• Object Oriented: Java is an object oriented programming language.
Almost everything in java is an object. All programs code and data
reside within objects and classes and all classes are arranged in
packages that we can use in our programs by inheritance.
• Robust & Secure: Java is a robust language. It provides many
safeguards to ensure reliable code. It has strict compile time and run
time checking for data types. Java also incorporates the concepts of
exception handlings which captures series errors and minimize any risk
for crashing the system.
Security becomes an important issue for a language which will be used
to programming on Internet. Threat of viruses is everywhere. Java
systems not only verify all memory access but also ensure that no
viruses are communicated with an applet. The absence of pointer in
Java ensures that programs cannot gain access to memory location
without proper authorization.
• Distributed: Java is designed as a distributed language for creating
application on network. Java application can open and access remote
object on internet as easily as they can do in a local system. This
enables multiple programmers at multiple remote locations to
collaborate and work together on a single project.
• Dynamic & Extensible: Java is a dynamic language. It is capable of
dynamically linking in new class Libraries, methods and objects.
Java programs supports functions written in other languages such as C and
C++. These functions are known as native methods. This facility enables the
programmers to use the efficient functions available in this language. Native
functions are linked dynamically at routine.
• Therefore from here we can suggest the following:
• Java is platform independent.
• Java is much more secure than other languages.
• Java is a network based language.
• Java is basically a server side language.
DATA TYPES
• Integer
– int 32 bits
– long 64 bits
– short 16 bits
– byte 8 bits
• Character
– char 16 bits, UNICODE Character
– String (is an object in Java)
• Floating Point
– float 32 bits
– double 64 bits
– Boolean
– true false
• Floating point literal includes either a decimal point or one of the following:
– E or e (add exponential value)
– F or f (float)
– D or d (double)
• 3.14 A simple floating-point value (a double)
• 6.02E23 A large floating-point value
• 2.718F A simple float size value
• 123.4E+306D A large double value with redundant
This Keywords: Sometimes a method will need to refer to the object that
invoke it, to allow this java defines the this keyword. It can be used inside any
methods to refer to the current object.
In other word this lets you refer directly to the object, to use it resolve any
name space collisions that might occur between instance variable and local
variables.
METHOD OVERLOADING
When we declared multiple methods with the same name but different
arguments list in the same class or in a sub class, such methods are called
overloaded methods. When the compiler sees the use of overloaded
methods, it uses the supplied arguments in the call to determine which
methods to invoke. This decision is made entirely at compilation time.
Method overloading is one of the way that java implements polymorphism.
ACCESS MODIFIER
Access to member variables and methods in java class is accomplished through
access modifiers. Access modifiers define the varying levels of access
between class members and outside world. Access modifiers are declared
immediately before the data type of a member variable or the return type of
a method.
There are four types of access modifiers: default, private, protected and public.
Default: Only class in the same physical file or compilation unit have access to
member variables and methods with default access specifies.
Private: Private variables and methods are accessible only within the class in
which it is declared.
Protected: Protected variables and methods are accessible only in the class in
which it is declared and also in the classes derived from it.
Public: Public class members and methods are accessible anywhere both inside
and outside the class in which it is declared.
Super: Sometimes it is needed to create a super class that keeps the details
of its implementation to itself that is private. In this case there is no way for a
subclass to access the member directly, as encapsulation is the primary attribute
of oop. Java provides a solution to this problem. Whenever a subclass needs to
refer to its immediate super class it can do so by use of keyboard (Super).
Super has two general forms, the first calls the super class constructor. The
second is used to access a member of the super class that has been hidden by a
member of a subclass.
Overriding Methods
In addition to producing a new class based on an old one by adding additional
features, you can modify existing behavior of the parent class.
If a method is defined in a subclass so that the name, return type, and
argument list exactly match those of a method in the parent class, then the
new method is said to override the old one.
The version of the method defined by the super class will be hidden.
Overridden methods in a class hierarchy is one of the ways that Java
implements the “single interface, multiple implementations” aspect of
polymorphism
FINAL VARIABLE METHOD & CLASS
All methods and variables can be overridden by default in a subclass. If we
wish to prevent the subclass from overriding the members of the super class,
we can declare them as final using keyword final as a modifier.
Example- final int variablename=value;
Final void methodName();
Making a method final ensures that the functionality defined in this method
will never be altered in any way. Similarly the value of final variable can never
be changed.
Final classes- sometimes we may like to prevent a class being further sub
classed for security reason. A class that cannot be sub classed is called a final
class.
Example – final class A
Final class B extends A
Any attempt to inherit this class will cause an error and the compiler will not
allow it. Declaring a class final prevents any unwanted extensions to the class.
Package- One of the main features of oop is its ability to reuse the code
already created. One way of achieving this is by extending the class and
implementing the interface we had created. This is limited to reusing the classes
within a program. But if we need to use classes from other programs without
physically copying them into the program can be accomplished in java by what is
known as package, a concept similar to class libraries in C, C++ or other language.
Some Java System Packages
• Java.lang: These are classes that java compiler itself uses and therefore
they are automatically imported. They include classes for primitive types,
string, math functions, threads and exceptions.
• Java.util: Language utility classes such as vectors, hash tables, random
numbers, date etc.
• Java.io: Input/output support classes. They provide facilities for input and
output of data.
• Java.awt: Set of classes for implementing graphical user interface. They
include class for windows, buttons, lists, menus and so on.
• Java.net: Classes for networking. They include classes for communicating
with local computer as well as with internet servers.
• Java.applet: Classes for creating and implementing applets.
What is CLASSPATH?
• CLASSPATH is an environment variable that tells the Java runtime system
where the classes are present
• When a packages is not created, all classes are stored in the default
package
• The default package is stored in the current directory.
• The current directory is the default directory for CLASSPATH.
• When you create your own package for example MyPack, all the .class files
including MyClass are saved in the directory MyPack.
• In order for a program to find MyPack, one of two things must be true:
– Either the program is executed from a directory immediately above
MyPack, or
– CLASSPATH must be set to include the path to MyPack
INTERFACE
Using the keyword interface, you can fully abstract a class interface from its
implementation. That is using interface, you can specify what a class must do,
but not how it does it. Interfaces are syntactically similar to classes, but they
lack instance variables, and their methods are declared without any body. In
practice this means that you can define interfaces which don’t make
assumptions about how they are implemented. Once it is defined, any
number of classes can implement an interface. Also, one class can implement
any number of interfaces
– An interface can include constant declarations
– An interface is a complex data type similar to class in Java.
– An interface is syntactically similar to an abstract class
– An interface is a collection of abstract methods and final
variables
• A class implements an interface using the implements clause.
– An interface lays the specification of what a class is
supposed to do
– An interface tells the implementing class what behaviours
to implement
– How the behaviour is implemented is the responsibility of
each implementing class
– Any class that implements an interface adheres to the
protocol defined by the interface, and in the process,
implements the specification laid down by the interface
Interfaces – Alternative to Multiple Inheritance
• Why are interfaces required when you have to provide method
implementation in the class? Why not write the methods directly in the
class?
• The answer is – If you have to use certain methods of class A in class B, you
will extend class B from class A, provided, class A and class B are strongly
related
• If you want to use some methods of class A in class B without forcing an
inheritance relationship, you determine whether there exists a
collaboration between them, and define an association between them.
• Defining an interface has the advantage that an interface definition stands
apart from any class or class hierarchy
• Moreover, it consists of a specification of behaviour/s in the form of
abstract methods
• This makes it possible for any number of independent classes to implement
the interface
• Thus, an interface is a means of specifying a consistent specification, the
implementation of which can be different across many independent and
unrelated classes to suit the respective needs of such classes
• Java does not support multiple inheritance
• This is a constraint in class design, as a class cannot achieve the
functionality of two or more classes at a time
• Interfaces help us make up for this loss. A class can implement more
than one interface at a time
• Thus, interfaces enable you to create richer classes and at the same
time the classes need not be related
• The utility of abstract methods defined in an abstract superclass is
restricted to subclass derived from that abstract superclass, which in
turn will override these abstract methods
• On the other hand, abstract methods declared in an interface can be
defined by independent classes not necessarily related to one another
through a class hierarchy
• The “single-interface, multiple implementations” aspect of runtime
polymorphism that you observed earlier in a class hierarchy is given a
much wider import and context through interfaces
• Just as classes can be inherited, interfaces can also be inherited
• One interface can extend one or more interfaces using the keyword
extends
• When you implement an interface that extends another interface, you
should provide implementation for all the methods declared within the
interface hierarchy