12. OOP Concepts:
- The
different OOP Concepts are
i) Objects
ii) Classes
iii) Attributes
iv) Operations
v) Inheritance
vi) Polymorphism
vii) Encapsulation
viii)
Abstraction
ix) Dynamic
Binding
x) Message
Passing
i) Objects:
- Objects are basic run-time entities in an
object-oriented system.
(or)
Any real
world entity is called an object.
(or)
Objects are
the combination of data and methods.
Example: Person, Place, bank account, …., so on.
- In the real-world only objects are visible but
classes are invisible.
- The most important benefits of an objects are
-
Modularity
-
Reusability
- The properties of objects are two types
-
visible
-
invisible
- Let man is an object, then visible properties are
eyes, ears, hands, legs,…so on and
invisible properties are name, blood group,…. so on.
- Every object contains three basic elements
-
Identity (name)
-
State (variables)
-
Behavior (methods)
ii) Classes:
- A class is defined as collection of similar
objects.
- Classes are user-defined data types and behave
like the built-in types of a programming language.
- In the real-world, classes are invisible only objects
are visible.
Example:
- man is an object representing a class called
Animal.
- We can see the object called man but we cannot see
the class called Animal.
Syntax:
Animal man;
- It will create an object man belonging to the
class Animal.
iii) Attributes:
- A collection of data values that describe a class.
iv) Operations:
- Also called as methods or services.
- It provides a representation of one of the
behaviors of a class.
v) Inheritance:
- Inheritance is
the way of producing new classes from already existing classes.
OR
- Inheritance is
the process by which objects of one class acquire the properties of objects of
another class
- Inheritance
supports the concept of hierarchical classification.
- The newly
created class is also called as sub class or child class or derived class.
- The old or
existing class is also called as super class or parent class or base class.
- Inheritance is
of 5 types
i) Single Inheritance
ii) Multi-level Inheritance
iii) Multiple Inheritance
iv) Hierarchical Inheritance
- Java
does not support Hybrid Inheritance.
vi) Polymorphism:
- Polymorphism means the ability to take more than
one form.
- Polymorphism is a combination of two Greek words,
Poly means “Many” and Morphos means “Forms”.
- Polymorphism means acting in many forms.
- It is of two types
a)
Static Polymorphism
b)
Dynamic Polymorphism
- Static Polymorphism supports Overloading.
- Dynamic Polymorphism supports Overriding
Overloading:
- Overloading means “Extending the existing meaning
by appending or adding new meaning”
- Overloading is of two types
-
Function or Method Overloading
-
Operator Overloading
- Java doesn’t support Operator Overloading.
Overriding:
- Overriding means “Replacing the existing meaning
with new meaning”.
vii) Encapsulation:
- The wrapping up of data and methods into a single
unit is known as Encapsulation.
- Data Encapsulation is possible by using the
concept called class.
- Data Encapsulation is also makes it possible to
create user-defined data types.
- It provides the concept of Data Hiding.
viii) Abstraction:
- Hiding Implementation details and only showing
essential details representing an abstraction.
- Data Abstraction is possible by using the concept
called class.
- By using data abstraction, we can separate
implementation details with interface details.
ix) Dynamic Binding:
- Binding refers to the linking of a procedure call
to the code to be executed in response to the call.
- Dynamic Binding Means that the code associated
with a given procedure call is not known until the time of the call at runtime.
- It is associated with polymorphism and
inheritance.
x) Message Passing:
- An
object-oriented program consists of a set of objects that communicate with each
other.
-
Objects communicate with one another by sending and receiving information much
the same way as people passes messages.
13. basics of objects and Classes in java:
i)
Introduction:
-
Classes create objects and objects use methods to communicate between them.
- A
class is a way to combine the variables with its associated methods and making
it as a single unit.
-
Calling a specific method in an object is described as sending the object a
message.
i)
Defining a class:
Syntax:
class classname
[extends superclassname]
{
[variable declaration;]
[method declaration;]
}
-
Everything inside the square brackets is optional.
- Empty
class declaration is
class classname
{ }
- In java,
the variables in a class are called as instance variables or fields or data
members.
- The
methods are called as instance methods or member functions.
- The
keyword extends indicates that the properties of the superclassname class are
extended to the classname class.
-
Classname and Superclassname are any valid java identifiers.
ii)
Adding Variables:
- Data is encapsulated in a class by placing
data fields inside the body of the class definition.
- These
variables are instance variables or member variables, because they are created
whenever an object of the class is instantiated.
Example:
class Rectangle { int length, width; }
- The
class Rectangle contains two integer type instance variables.
iii)
Adding Methods:
-
Methods are declared inside the body of the class but immediately after the
declaration of instance variables.
Syntax: (method definition)
return_type method_name(Parameters_list)
{
method body;
……….
……….
return (expression);
}
Example:
class
Rectangle
{
int l,b;
void getData(int x, int y)
{
l=x;
b=y;
}
int area()
{
return (l*b);
}
}
- The
method getData() has return type void represents it doesn’t return any value.
iv)
Creating Objects:
-
Creating an object is also referred to as instantiating an object.
-
Objects in java are created dynamically using the new operator.
- The
new operator creates an object of the specified class and returns a reference
to that object.
Syntax:
(creating an object)
classname objectreference=new
classname();
Example:
Rectangle r1=new Rectangle();
v)
Accessing Class Members:
- To
access class members, we must use the concerned object and dot operator.
Syntax:
objectreference.variablename;
objectreference.methodname(parameters_list);
- Here,
the object name is the name of the object.
-
variablename is the name of the instance variable inside an object.
-
methodname is the method inside an object.
-
Parameters_list is a separate list of actual values.
Example:
r1.l;
r1.b;
r1.getData(10,20);
r1.area();
Example
Program:
class Rectangle
{
int
l,b;
void getData(int x, int y)
{
l=x;
b=y;
}
int area()
{
return(l*b);
}
}
class RectArea
{
public static void main(String args[])
{
int rarea;
Rectangle r1=new Rectangle();
r1.getData(10,20);
rarea=r1.area();
System.out.println("Area of
Rectangle="+rarea);
}
}
Output:
Area of
Rectangle=200
14.
METHOD OVERLOADING:
- In
java, it is possible to create methods with same name, different parameters and
different definitions, represents Method Overloading.
-
Method Overloading is used when objects are required to perform similar tasks
but using different input parameters.
- It
supports static polymorphism.
- Java
does not support Operator Overloading.
Example:
class Overload
{
void test()
{
System.out.println("No
parameters");
}
void test(int a)
{
System.out.println("a="+a);
}
void test(int a, int b)
{
System.out.println("a="+a+"b="+b);
}
double test(double x,double y)
{
return(x+y);
}
}
class MO
{
public static void main(String args[])
{
Overload obj=new Overload();
obj.test();
obj.test(10);
obj.test(7,31);
System.out.println("Sum of two
double values="+obj.test(12.3,34.6));
}
}
Output:
a=10
a=7b=31
Sum of two double values=46.90000000000000
15.
CONSTRUCTORS:
- A constructor is
a special method of the class and it is used to initialize an object whenever
the object is created.
- A Constructor is
a special method because,
·
Class name and
Constructor name both must be same
·
Doesn’t contain return
type
·
Automatically executed.
- Constructors are
three types
i) Default Constructor
ii) Parameterized Constructor
iii) Dummy or Copy Constructor
- Copy constructor
is not available in java.
i)
Default Constructor:
- There is no parameters
used in default constructor.
Example:
class Perimeter
{
Perimeter()
{
System.out.println("Default
Constructor means no parameters");
}
}
class DC
{
public static void main(String args[])
{
Perimeter p1=new Perimeter();
}
}
ii)
Parameterized Constructor:
Example:
class Perimeter
{
Perimeter(int l, int b)
{
System.out.println("Perimeter of the
Rectangle="+(2*(l+b)));
}
}
class CO
{
public static void main(String args[])
{
Perimeter p1=new Perimeter(10,20);
}
}
16.
CONSTRUCTOR OVERLOADING:
- Like methods,
constructors can also be overloaded.
- Constructor
overloading is the process of writing more than one constructor with the same
name but with different types and different number of arguments.
Example:
class Perimeter
{
Perimeter()
{
System.out.println("No
parameters");
}
Perimeter(double r)
{
System.out.println("Perimeter of the
Circle="+(2*3.14*r));
}
Perimeter(int l, int b)
{
System.out.println("Perimeter of the
Rectangle="+(2*(l+b)));
}
}
class CO
{
public static void main(String args[])
{
Perimeter p1=new Perimeter();
Perimeter p2=new Perimeter(10);
Perimeter p3=new Perimeter(10,20);
}
}
Output
No parameters
Perimeter of the
Circle=62.80000000000000
Perimeter
of the Rectangle=60
17. Finalizer in Java: (Java finalize() Method)
- A garbage
collector frees the memory used by objects that are no longer needed.
- It is difficult
for the programmer to forcefully execute the garbage collector to destroy the
object.
- But Java
provides an alternative way to do the same.
- The Java Object
class provides the finalize() method that works the same as
the destructor.
Syntax:
protected void finalize throws Throwable()
{
//resources to be close
}
- It is
a protected method of the Object class that is defined in the java.lang package.
- It can be called only once.
- We need to call the finalize() method explicitly if
we want to override the method.
- The gc() is a method of JVM executed by the Garbage
Collector. It invokes when the heap memory is full and requires more memory for
new arriving objects.
- Except for the unchecked exceptions, the JVM
ignores all the exceptions that occur by the finalize() method.
Example
Program:
public class A
{
public static void main(String[] args)
{
A obj = new A();
obj.finalize();
obj = null;
System.gc();
System.out.println("Inside the main() method");
}
protected void finalize()
{
System.out.println("Object is destroyed by the Garbage Collector");
}
}
Output:
Object
is destroyed by the Garbage Collector
Inside
the main() method
Object
is destroyed by the Garbage Collector
18. Visibility Modifiers:
- Also called as Access Modifiers.
- There are four types of Java Visibility
modifiers:
i) Private:
- The access level of a private modifier
is only within the class.
- It cannot be accessed from outside the
class.
ii) Default:
- The access level of a default modifier
is only within the package.
- It cannot be accessed from outside the
package.
- If you do not specify any access level,
it will be the default.
iii) Protected:
- The access level of a protected
modifier is within the package and outside the package through child class.
- If you do not make the child class, it
cannot be accessed from outside the package.
iv) Public:
- The access level of a public modifier
is everywhere.
- It can be accessed from within the
class, outside the class, within the package and outside the package.
Understanding
Java Access Modifiers:
Access
Modifier |
within class |
within package |
outside package by subclass only |
outside package |
Private |
Y |
N |
N |
N |
Default |
Y |
Y |
N |
N |
Protected |
Y |
Y |
Y |
N |
Public |
Y |
Y |
Y |
Y |
19. String Handling in java:
- A
String is a group of characters enclosed within double quotes.
-
String is a class in java from a package java.lang.
-
String is a final class, it means we cannot create any child classes by
extending this class.
-
String in java uses two classes to create string objects.
i) String class
ii) StringBuffer class
i)
String class:
- A
string class creates immutable objects, i.e., we cannot change or modify a
string during execution of a program.
- The
different methods in String class are,
Method |
Usage |
charAt() |
used to extract a single character from
a string |
getChars() |
extracting more than one character |
compareTo() |
used to determine whether the string is
less than, equal to or greater than the invoking string |
concat() |
used to concatenate two strings |
substring() |
used to extract a part of a string |
ii)
StringBuffer class
- StringBuffer class creates mutable objects, i.e., we
can change or modify a string during execution.
- It is a peer class of string from a package
java.lang.
- StringBuffer is a final class, which means we cannot
create any child classes from it.
- Initially StringBuffer allocates an extra memory of
16 characters for the existing string in order to allow modifications on the
string value.
- The different methods in StringBuffer class are,
Method |
Usage |
length() |
used to return length of a StringBuffer |
capacity() |
used to return the total allocated capacity |
setLength() |
sets the length of the buffer within a StringBuffer
object |
ensureCapacity() |
used to set the size of the Buffer after
StringBuffer is created |
delete() |
deletes the substring from the invoking string |
setCharAt() |
sets the value of a character at the specified
location within a StringBuffer |
deleteCharAt() |
deletes a single character at the specified index
from the invoking object |
append() |
appends or concatenates the string representation of
any datatype to the end of invoking StringBuffer object |
charAt() |
obtains a single character at the specified location
or index from StringBuffer |
getChars() |
copy a substring of a StringBuffer into an array |
20. Java Character class:
- Java provides wrapper class Character for
primitive data type char.
The
Character class offers a number of useful class (i.e., static) methods for
manipulating characters. You can create a Character object with the Character
constructor −
Character
ch = new Character('a');
i)
Character class methods:
S.No. |
Method |
Description |
1 |
Determines
whether the specified char value is a letter. |
|
2 |
Determines
whether the specified char value is a digit. |
|
3 |
Determines
whether the specified char value is white space. |
|
4 |
Determines
whether the specified char value is uppercase. |
|
5 |
Determines
whether the specified char value is lowercase. |
|
6 |
Returns
the uppercase form of the specified char value. |
|
7 |
Returns
the lowercase form of the specified char value. |
|
8 |
Returns a String
object representing the specified character value that is, a one-character
string. |
21. Java File class:
- Java
File class represents the files and directory pathnames in an abstract manner.
- This
class is used for creation of files and directories, file searching, file
deletion, etc.
- The
File object represents the actual file/directory on the disk.
File
class methods:
S.No. |
Method |
Description |
1 |
public
String getName()
|
Returns
the name of the file or directory denoted by this abstract pathname. |
2 |
public
String getParent()
|
Returns
the pathname string of this abstract pathname's parent, or null if this
pathname does not name a parent directory. |
3 |
public
File getParentFile() |
Returns
the abstract pathname of this abstract pathname's parent, or null if this
pathname does not name a parent directory. |
4 |
public
String getPath() |
Converts
this abstract pathname into a pathname string. |
5 |
public
boolean isAbsolute()
|
Tests
whether this abstract pathname is absolute. Returns true if this abstract
pathname is absolute, false otherwise. |
6 |
public
String getAbsolutePath() |
Returns
the absolute pathname string of this abstract pathname. |
7 |
public
boolean canRead()
|
Tests
whether the application can read the file denoted by this abstract pathname.
Returns true if and only if the file specified by this abstract pathname
exists and can be read by the application; false otherwise. |
8 |
public
boolean canWrite()
|
Tests
whether the application can modify to the file denoted by this abstract
pathname. Returns true if and only if the file system actually contains a
file denoted by this abstract pathname and the application is allowed to
write to the file; false otherwise. |
22. this reference in Java: (this keyword)
- There will be situations where a method
wants to refer to the object which invoked it.
- To perform this, we use this keyword.
- this keyword is used to represent the
current object of the class
Example:
class Sample
{
int a,b;
Sample(int a, int b)
{
this.a=a;
this.b=b;
}
}
class ThisDemo
{
public static void main(String args[])
{
Sample s1=new Sample(10,20);
System.out.println("Sum of two numbers="+(s1.a+s1.b));
}
}
Output:
Sum of two numbers=30
- Here, instance variables and local
variables have the same name, i.e., a and b.
No comments:
Post a Comment