0% found this document useful (0 votes)
55 views

OOP Stands For Object-Oriented Programming

OOP stands for object-oriented programming. It involves creating objects that contain both data and methods, unlike procedural programming which focuses on writing procedures or methods that operate on data. OOP has advantages like being faster, providing clearer structure, keeping code DRY, and enabling reusable applications with less code. Abstraction hides details and shows essential information. Encapsulation binds code and data by protecting data within a class from external access except through member functions. Polymorphism allows performing the same action in different ways by inheriting and overriding methods.

Uploaded by

Anjani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

OOP Stands For Object-Oriented Programming

OOP stands for object-oriented programming. It involves creating objects that contain both data and methods, unlike procedural programming which focuses on writing procedures or methods that operate on data. OOP has advantages like being faster, providing clearer structure, keeping code DRY, and enabling reusable applications with less code. Abstraction hides details and shows essential information. Encapsulation binds code and data by protecting data within a class from external access except through member functions. Polymorphism allows performing the same action in different ways by inheriting and overriding methods.

Uploaded by

Anjani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that


perform operations on the data, while 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

Abstraction:

Data abstraction is the process of hiding certain details and showing only
essential information to the user.
Abstraction can be achieved with either abstract classes or Interfaces

The abstract ket word iis a non-access modifier, used for classes and
methods:

● Abstract class: is a restricted class that cannot be used to create


objects (to access it, it must be inherited from another class).

Abstract method: can only be used in an abstract class, and it does not
have a body. The body is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods:

abstract class Animal {

// Abstract method (does not have a body)

public abstract void animalSound();

// Regular method

public void sleep() {


System.out.println("Zzz");

// Subclass (inherit from Animal)

class Pig extends Animal {

public void animalSound() {

// The body of animalSound() is provided here

System.out.println("The pig says: wee wee");

class Main {

public static void main(String[] args) {

Pig myPig = new Pig(); // Create a Pig object

myPig.animalSound();

myPig.sleep();

Encapsulation:

Encapsulation is defined as the wrapping up of data under a single unit. It is


the mechanism that binds together code and the data it manipulates. Another
way to think about encapsulation is, that it is a protective shield that prevents
the data from being accessed by the code outside this shield.
● Technically in encapsulation, the variables or data of a class is
hidden from any other class and can be accessed only through any
member function of its own class in which it is declared.
● As in encapsulation, the data in a class is hidden from other classes
using the data hiding concept which is achieved by making the
members or methods of a class private, and the class is exposed to
the end-user or the world without providing any details behind
implementation using the abstraction concept, so it is also known as
a combination of data-hiding and abstraction.
● Encapsulation can be achieved by Declaring all the variables in the
class as private and writing public methods in the class to set and
get the values of variables.
● It is more defined with the setter and getter method

// constructor to initialize values

Area(int length, int breadth) {

this.length = length;

this.breadth = breadth;

}
// method to calculate area

public void getArea() {

int area = length * breadth;

System.out.println("Area: " + area);

class Main {

public static void main(String[] args) {


Area rectangle = new Area(2, 16);

rectangle.getArea();

Polymorphism means "many forms", and it occurs when we have many


classes related to each other by inheritance.

As we specified in the previous chapter; Inheritance lets us inherit attributes


and methods from another class. Polymorphism uses those methods to
perform different tasks. This allows us to perform a single action in different
ways.

For example, think of a superclass called Animal that has a method called

animalSound() Subclasses of Animals could be Pigs, Cats, Dogs, and Birds -


And they also have their own implementation of an animal sound (the pig
oinks, and the cat meows, etc.):

// fields to calculate area

class Area {
int length;

int breadth;

// constructor to initialize values

Area(int length, int breadth) {

this.length = length;

this.breadth = breadth;

// method to calculate area

public void getArea() {

int area = length * breadth;

System.out.println("Area: " + area);

class Main {

public static void main(String[] args) {


Area rectangle = new Area(2, 16);

rectangle.getArea();

You might also like