Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

OBJECT -ORIENTED PROGRAMMING

• Object-oriented programming is basically a computer programming design


philosophy or methodology that organizes/ models software design around
data, or objects rather than functions and logic.
• Object-oriented programing is a programing paradigm based on the concept
of “objects”, which can contain data and code: data in the form of fields,
and code, in the form of procedures. A common feature of objects is that
procedures are attached to them and can access and modify the objects
data field.
• An object is referred to as a data field that has unique attributes and
behavior. Everything in OOP is grouped as self-sustainable objects.
• It is the most popular programming model among developers. It is well
suited for programs that are large, complex, and actively updated or
maintained. It simplifies software development and maintenance by
providing major concepts such as Abstraction, Inheritance, Polymorphism,
and Encapsulation. These core concepts support OOP

THE MAIN OOP PRINCIPEL


• All object-oriented programming languages provide mechanisms that help
you implement the object-oriented model. They are
Encapsulation,Abstraction, Inheritance, and Polymorphism. Let’s take a look
at these concepts now.
  ENCAPSULATION
Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside inheritance and misuse. One
way to think about encapsulation is as a protective wrapper that prevents the
code and data from being arbitrarily access by other code defined outside the
wrapper. Access to the code and data inside the wrapper is tightly controlled
through a well-defined interface.
In Java the basis of encapsulation is the class. A class defines the structure
and behaviour (data and code) that will be shared by a set of objects. Each
object of a given class contains a structure and behaviour defined by the
class, as if it were turned out by a mold in the shape of the class. For this
reason objects are sometimes referred to as instance of a class. Thus, a class
is a logical construct; an object has physical reality.
  INHERITANCE
inheritance is the process by which one object acquires the properties of
another object. This is important because it supports the concepts of
hierarchical classification. However, by use of inheritance, an object need
only define those qualities that make it unique within its class. It can inherit
its general attributes from its parent. Thus, it is the inheritance mechanism
that makes it possible for one object to be a specific instance of a more
general case.
  POLYMORPHISM
Polymorphism (form the Greek, meaning “many forms”) is a feature that
allows one interface to be used for a general class of actions. The specific
action is determined by the exact nature of the situation. Consider a stack
(which is a last-in, first-out list). You might have a program that requires three
types of stacks. One stack is used for integer values, one for floating-point,
and one for characters. The algorithm that implements each stack is the
same, even through the data being stored differs. In a non-object-oriented
language, you would be required to create thee different sets of stack
routines, with each set using different names. However, because of
polymorphism, in Java you can specify a general set of stack routines that all
share the same names.

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

CLASS & OBJECT


A class is a template or prototype that defines a type of object. Just as a
draftsman can create a blueprint for a device that can be used many times to
construct actual devices, a class is a software blueprint that can be used to
instantiate many individual objects. A class defines the set of data element
(attributes) that defines the objects as well as the set of behaviors or
functions (called Method) that manipulate the objects or perform interaction
between related objects.
So a class describes the data that each object includes. A class describes the
behaviors that each object exhibits.
<Syntax> of Java class
<modifier>class<class_name> {
<attribute_declaration>
<constructor_declaration>
<method_declaration>
}
There are two kinds of thing that can be included in a class definition:-
Fields: These are variables that stare data items that typically differentiate one
object of the class from another. They are also referred to as data member of
a class.
Methods: These define the operations perform for the class. Methods
typically operate on the fields.
Variable in Class Definition
An object of a class is also referred to as instance of that class. When you
create an object, the object will contain all the variable that was included in
the class definition.
The variable in a class are of two types:
Instance variable & Class variable.
Instance Variable: Each objects of the class will have its own copy of each of
the instance variable that appears in each class definition.
An instance variable is declared within the class in the usual work with type
name and variable name and can have initial value specified.
Class Variable: A given class will only have one copy of each of its class
variable and these will be shared between all the objects of the class. These
class variables exist even if no objects of the class have been created.
If the value of a class variable is changed, the new value is available in all the
objects of the class.
A class variable declared with keyword (Static).

Constructor: Java allows objects to initialize themselves when they are


created. This automatic initialization is performed through the use of a
constructor.
In other word constructor allows to perform initialization of member variables
and perform any other operations when an object is created from a class.
Constructors are always given the same name as that of the class.
Constructors do not have a return type and not even void.
 

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.

Inheritance: Inheritance can be defined as the process where one class


acquires the properties of another. The class which inherit the properties of other
is known as sub class(derived class, child class)and the class whose properties are
inherited is known as super class(base class, parent class).
 

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.

Abstract Method and classes


Sometimes we create a super class that only defines a generalized form that
will be shared by all of its subclasses leaving it to each subclass to fill in the
details. Such a class determines the nature of the methods that the subclass must
implement.
An abstract class indicates that a method must always be redefined in a
subclass, thus making overridden compulsory.
Abstract classes are classes that are partially implemented.
Abstract methods are method that are declared but not implemented.
We cannot make constructor function abstract, we cannot make static
method abstract, and cannot make private methods abstract.
The objective is to:
– Create a superclass that only defines a generalized form
that will be shared by all of its subclasses
– leaving it to each subclass to provide for its own specific
implementations
– Such a class determines the nature of the methods that the
subclasses must implement
– Such a superclass is unable to create a meaningful
implementation for a method or methods
Any class that contains one or more abstract methods must also be declared
abstract
– A class is declared as an abstract class by preceding the keyword
abstract before the class keyword at the beginning of the class
declaration
– There can be no objects of an abstract class
– An abstract class, cannot, therefore, be instantiated with the new
keyword
– Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be itself declared abstract
– It is perfectly acceptable for an abstract class to implement a
concrete method

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

You might also like