Object Oriented Programming in Java

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Object Oriented

Programming
Java
Java - What is OOP?
 object-oriented programming is about creating objects that
contain both data and methods.
Object-oriented programming has several advantages over
procedural programming:

•OOP is faster and easier to execute


•OOP provides a clear structure for the programs
•OOP helps to keep the Java code DRY "Don't Repeat
Yourself", and makes the code easier to maintain,
modify and debug
•OOP makes it possible to create full reusable
applications with less code and shorter development
time
Class and Object

 Class is a set of object which shares common characteristics/


behavior and common properties/ attributes.
Everything in Java is associated with classes and objects, along
with its attributes and methods. For example: in real life, a car
is an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for
creating objects.
To create a class, use the keyword class:

public class Main


{ int x = 5;
}
To create an object called "myObj" and print the value of x:

public class Main { int x = 5;


Public static void main(String[]args)
{
Main myObj = new Main();
System.out.println(myObj.x);
} }
Inheritance in Java:

is a mechanism in which one object acquires all the


properties and behaviors of a parent object.
The idea behind inheritance in Java is that you can
create new classes that are built upon existing classes
also known as a parent-child relationship.

Why use inheritance in java


•For Method Overriding (so runtime polymorphism can be
achieved).
•For Code Reusability.

Example
Java Inheritance Example

class Employee{  
 float salary=40000;  
}  
class Programmer extends Employee{
  
 int bonus=10000;  
 public static void main(String args[])
{  
   Programmer p=new Programmer();  
   System.out.println("Programmer
 salary is:"+p.salary);  
   System.out.println("Bonus of
 Programmer is:"+p.bonus);  
}   Programmer is the subclass/child
}   class and Employee is the super
class/parent class. The relationship
between the two classes
is Programmer IS-A Employee. 
Polymorphism
Polymorphism is the ability of an object to take on different forms. In
Java, polymorphism refers to the ability of a class to provide different
implementations of a method, depending on the type of object that is
passed to the method.

Abstraction
 is a process of hiding the implementation details and showing only
functionality to the user.
Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where you type the text
and send the message. You don't know the internal processing about
the message delivery.
Encapsulation in Java
 is a process of wrapping code and data together into a single unit,
for example, a capsule which is mixed of several medicines.

The Java Bean class is the example of a fully encapsulated class.

We can create a fully


encapsulated class in Java by
making all the data members
of the class private. Now we
can use setter and getter
methods to set and get the
data in it.
Access Modifiers in Java
There are four types of Java access modifiers:

Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
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.
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.
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.
Java Constructors

A constructor in Java is a special method that is used to initialize


objects. The constructor is called when an object of a class is created. It
can be used to set initial values for object attributes:

// Create a Main class


public class Main {
int x; // Create a class attribute
// Create a class constructor for the Main class
public Main() {
x = 5; // Set the initial value for the class
attribute x }
public static void main(String[] args) {
Main myObj = new Main(); // Create an object of
class Main (This will call the constructor)
System.out.println(myObj.x); // Print the value of
x } } // Outputs 5

You might also like