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
• 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.
• 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