OOPS in Java - Lecture 21
OOPS in Java - Lecture 21
JAVA
Object-Oriented Programming is a methodology or paradigm to design a
programusingclassesandobjects.Itsimplifiesthesoftwaredevelopment
and maintenance by providing some concepts defined below :
Example 1:
class
Student
{
String
name
;
int
age
;
public
void
getInfo
() {
System
.
out
.
println
(
"
The name of this Student
is "
+
this
.
name
);
System
.
out
.
println
(
"
The age of this Student
is "
+
this
.
age
);
}
}
public
class
OOPS
{
public
static
void
main
(
String
args
[]) {
Student
s1
=
new
Student
();
s1
.
name
=
"Aman"
;
s1
.
age
=
24
;
s1
.
getInfo
();
Student
s2
=
new
Student
();
s2
.
name
=
"Shradha"
;
s2
.
age
=
22
;
s2
.
getInfo
();
}
}
Example 2:
class
Pen
{
String
color
;
public
voidprintColor() {
System
.
out
.
println
(
"
The color of this Pen is
"
+
this
.
color
);
}
}
public
classOOPS{
public
static
void main
(
String
args
[]) {
Pen
p1
=
new Pen
();
p1
.
color
= blue;
en
P p2
=
new
Pen
();
p2
.
color
= black;
en
P p3
=
new
Pen
();
p3
.
color
= red;
1
p .
printColor
();
p2
.
printColor
();
p3
.
printColor
();
}
}
Student
() {
System
.
out
.
println
(
"
Constructor called"
);
}
}
Student
(
S
tring
name
,
int
age
) {
this
.
name
=
name
;
this
.
age
=
age
;
}
}
Student
(
S
tudent
s2
) {
this
.
name
=
s2
.
name
;
this
.
age
=
s2
.
age
;
}
}
Types of PolymorphismIMP
class
Student
{
String
name
;
int
age
;
public
void
displayInfo
(
String
name
) {
System
.
out
.
println
(
name
);
}
public
void
displayInfo
(
int
age
) {
System
.
out
.
println
(
age
);
}
public
void
displayInfo
(
String
name
,
int
age
) {
System
.
out
.
println
(
name
);
System
.
out
.
println
(
age
);
}
}
class
Shape
{
public
void
area
() {
System
.
out
.
println
(
"Displays Area of Shape"
);
}
}
class
Triangle
extends
Shape
{
public
void
area
(
int
h,
int
b
) {
System
.
out
.
println
((
1
/
2
)*
b
*
h
);
}
}
class
Circle
extends
Shape
{
public
void
area
(
int
r) {
System
.
out
.
println
((
3.14
)*
r
*
r
);
}
}
Inheritance
Inheritance is a process in which one object acquires all the properties and
ehaviors of its parent object automatically. In such a way, you canreuse,
b
extend ormodifythe attributes and behaviors whichare defined in other
classes.
In Java, the class which inherits the members of another class is called
derivedclassandtheclasswhosemembersareinheritediscalledbaseclass.
The derived class is the specialized class for the base class.
Types of Inheritance :
1.Single inheritance:When one class inherits anotherclass, it is known
as single level inheritance
class
Shape
{
public
void
area
() {
System
.
out
.
p
rintln
(
"Displays Area of Shape"
);
}
}
class
Triangle
extends
Shape
{
public
void
area
(
i
nt
h,
int
b
) {
System
.
out
.
p
rintln
((
1
/
2
)*
b
*
h
);
}
}
Package in Java
ackage is a group of similar types of classes, interfaces and sub-packages.
P
Packages can be built-in or user defined.
import
java
.
util
.
S
canner
;
import
java
.
io
.
I
OException
;
➢ P
rivate: The access level of a private modifier isonly within the class. It cannot
be accessed from outside the class.
➢ D
efault: The access level of a default modifier isonly within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default.
➢ P
rotected: The access level of a protected modifieris 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.
➢ P
ublic: 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.
package
newpackage
;
class
Account
{
public
String
name
;
protected
String
email
;
private
String
password
;
public
void
setPassword
(
String
password
) {
this
.
p
assword
=
password
;
}
}
public
class
Sample
{
public
static
void
main
(
String
args
[]) {
Account
a1
=
new
Account
();
a1
.
n
ame
=
"Apna College"
;
a1
.
s
etPassword
(
"abcd"
);
a1
.
e
mail
=
"[email protected]"
;
}
}
Encapsulation
Abstraction
e try to obtain anabstract view,model or structure of a real life problem,and
W
reduce its unnecessary details. With definition of properties ofproblems,
including the data which are affected and the operations whichare identified,
the modelabstracted from problems can be a standardsolution to this type of
problems. It is an efficient way since there arenebulousreal-life problems that
have similar properties.
In simple terms, it is hiding the unnecessary details & showing only the
essential parts/functionalities to the user.
●
An abstract class must be declared with an abstract keyword.
●
It can have abstract and non-abstract methods.
●
It cannot be instantiated.
●
It can have constructors and static methods also.
●
It can have final methods which will force the subclass not to change the body of
the method.
abstract
class
Animal
{
abstract
void
walk
();
void
breathe
() {
System
.
out
.
println
(
"
This animal breathes air"
);
}
Animal
() {
System
.
out
.
println
(
"
You are about to create
an Animal."
);
}
}
class
Horse
extends
Animal
{
Horse
() {
System
.
out
.
println
(
"
Wow, you have created a
Horse!"
);
}
void
walk
() {
System
.
out
.
println
(
"
Horse walks on 4 legs"
);
}
}
class
Chicken
extends
Animal
{
Chicken
() {
System
.
out
.
println
(
"
Wow, you have created a
Chicken!"
);
}
void
walk
() {
System
.
out
.
println
(
"
Chicken walks on 2 legs"
);
}
}
public
class
OOPS
{
public
static
void
main
(
String
args
[]) {
Horse
horse
=
new
Horse
();
horse
.
walk
();
horse
.
breathe
();
}
}
2.Interfaces
●
All the fields in interfaces are public, static and final by default.
●
All methods are public & abstract by default.
●
A class that implements an interface must implement all the methods declared
in the interface.
●
Interfaces support the functionality of multiple inheritance.
interface
Animal
{
void
walk
();
}
class
Horse
implements
Animal
{
public
void
walk
() {
System
.
out
.
println
(
"
Horse walks on 4 legs"
);
}
}
class
Chicken
implements
Animal
{
public
void
walk
() {
System
.
out
.
println
(
"
Chicken walks on 2 legs"
);
}
}
public
class
OOPS
{
public
static
void
main
(
String
args
[]) {
Horse
horse
=
new
Horse
();
horse
.
walk
();
}
}
Static Keyword
Static can be :
3. Block
class
Student
{
static
String
school
;
String
name
;
}
public
class
OOPS
{
public
static
void
main
(
String
args
[]) {
Student
.
school
=
"JMV"
;
Student
s1
=
new
Student
();
Student
s2
=
new
Student
();
s1
.
name
=
"Meena"
;
s2
.
name
=
"Beena"
;
System
.
out
.
println
(
s
1
.
school
);
System
.
out
.
println
(
s
2
.
school
);
}
}