KEMBAR78
OOP Intro | PDF | Object Oriented Programming | Class (Computer Programming)
0% found this document useful (0 votes)
11 views10 pages

OOP Intro

Object-Oriented Programming (OOP) organizes code using objects that represent real-world entities, emphasizing concepts like classes, encapsulation, inheritance, and polymorphism. Java is a high-level, object-oriented programming language that requires a development environment and allows for the creation of classes and objects. Key concepts include data types, attributes, and methods, which help structure and manage code effectively.

Uploaded by

nilbarua51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

OOP Intro

Object-Oriented Programming (OOP) organizes code using objects that represent real-world entities, emphasizing concepts like classes, encapsulation, inheritance, and polymorphism. Java is a high-level, object-oriented programming language that requires a development environment and allows for the creation of classes and objects. Key concepts include data types, attributes, and methods, which help structure and manage code effectively.

Uploaded by

nilbarua51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Object-Oriented Programming (OOP)

What is it?

OOP is a way of programming where we organize our code like a toy box, with "toys"
(objects) that have specific roles and behaviors. It’s like playing with building blocks
where each block has its own shape and purpose, and we combine them to build
something bigger.

Simula is considered the first object-oriented


programming language.

The popular object-oriented languages


are Java, C#, PHP, Python, C++, etc

The aim of object-oriented programming is to


implement real-world entities, for example,
object, classes, abstraction, inheritance,
polymorphism, etc.

Object Example: A dog is an object because it has states like color, name, breed, etc.
as well as behaviors like wagging the tail, barking, eating, etc.

Class: Collection of objects is called


class. It is a logical entity.

Encapsulation: Wrapping data (like a toy’s color or size) and actions (what the toy
does) together, like keeping a toy in a special box where only certain parts can be
touched.
A Java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.

Abstraction: Showing only the important parts of a toy, like how a car moves without
knowing how the engine works inside.

Inheritance: A new toy (like a racecar) can borrow features from an older toy (a regular
car) and add its own special tricks.

Polymorphism: Toys can act differently in different situations, like a doll that can sing or
talk depending on what you ask it to do.
OOP makes code easier to manage, reuse, and scale, like organizing a toy box so you
can quickly find and use the right toy.

What is Java?
Java is a programming language and a platform. Java is a high-level, robust,
object-oriented and secure programming language.
Since Java has a runtime environment (JRE) it needs a platform,
development environment and Java development typically requires the
Java Development Kit (JDK)
Once we have the JDK installed, you can use a text editor or an
Integrated Development Environment (IDE)

Java Hello World Program:


public class Main{
public static void main (String args[]) {
System.out.println("Hello World!");
}
}
Output : Hello World!
o class keyword is used to declare a class in Java.
o public keyword is an access modifier that represents visibility. It
means it is visible to all.
o static is a keyword. If we declare any method as static, it is known as
the static method. The core advantage of the static method is that
there is no need to create an object to invoke the static method. The
main() method is executed by the JVM, so it does not require creating
an object to invoke the main() method. So, it saves memory.
o void is the return type of the method. It means it does not return any
value.
o The main() method represents the starting point of the program.
o String[] args or String args[] is used for command line argument. We
will discuss it in coming section.
o System.out.println() is used to print statement on the console. Here,
System is a class, out is an object of the PrintStream class, println() is
a method of the PrintStream class. We will discuss the internal
working of System.out.println() statement in the coming section.

Objects and Classes


What are they?

• Class: A blueprint or template for creating something. It describes what an


object will look like and what it can do.

• Object: An actual thing created from the class blueprint, with specific details
filled in.

Exm: Think of a class as a cookie cutter shaped like a star. It’s the plan for making star-
shaped cookies. An object is the actual cookie you make using the cutter, and you can
decorate it with sprinkles or chocolate chips to make it unique.

Example in Real Life:

• Class: Car – A blueprint that says a car has wheels, a color, and can move.

• Object: My Red Car – A specific car made from the blueprint, with 4 wheels, red
color, and the ability to drive.
Values and Data Types
What are they?

• Values: The actual information stored in a program, like numbers (5, 3.14) or
words ("Hello").

• Data Types: The kind of box you use to store values, which determines what kind
of information can go inside and what you can do with it.

Example: Imagine you have different toy boxes for different toys:

• A small box for tiny marbles (like int for whole numbers).

• A bigger box for dolls with names (like String for text).

• A special box for toy boats that can float in water (like double for decimal
numbers). Each box (data type) can only hold certain things (values), and you
need the right box to keep things organized.

Java Data Types:


• Primitive Data Types: Basic building blocks.
o int: Whole numbers (e.g., 5, -10).
o double: Decimal numbers (e.g., 3.14, -0.5).
o char: Single characters (e.g., 'A', '7').
o boolean: True or false values.
• Non-Primitive Data Types: More complex, like String (for text) or user-defined
classes.
Diagrams and Programmed Process (DPPs)

Diagram 1: Class and Object Relationship

Diagram 2: Data Types


Creating a Class and Object
1. Define a Class: Write the blueprint (e.g., for a "Dog").
2. Add Attributes: Include properties like name and age.
3. Add Methods: Define actions like bark or sleep.
4. Create an Object: Use the blueprint to make a specific dog.
5. Use the Object: Access its attributes or call its methods.

Step-by-Step Coding Examples:


Q1: Creating a Class and Object
Program: Create a Dog class and make a dog object.
Output:
My dog's name is Buddy
My dog is 3 years old
Buddy says Woof!
Explanation:
• Class Dog: The blueprint defines what a dog has (name, age) and
does (bark).
• Object myDog: A specific dog created from the blueprint.
• Attributes: name and age are set to "Buddy" and 3.
• Method Call: bark() makes the dog "speak."
• Key Terms:
o new keyword: Creates an object from a class.
o Dot operator (.): Accesses attributes or methods of an object
(e.g., myDog.name).

Q2: Using Data Types

Program: Demonstrate primitive and non-primitive data types.


Output:
Name: Alice
Age: 15
Height: 5.5 feet
Grade: A
Is a student? true
Explanation:
• Primitive Types: int, double, char, and boolean store simple values
directly.
• Non-Primitive Type: String stores text and is an object.
• Variables: Named storage boxes (e.g., age, name) that hold values of
specific data types.
• Key Terms:
o Variable Declaration: Specifying the data type and name (e.g.,
int age).
o Initialization: Assigning a value (e.g., age = 15).
Questions and Answers
Here are some questions to reinforce understanding, with answers tailored
for clarity.
Q1: What is the difference between a class and an object? A: A class is
like a recipe for a cake, telling you what ingredients and steps to use. An
object is the actual cake you bake using that recipe, with specific flavors or
decorations.
Q2: Why do we use data types in Java? A: Data types are like different-
sized boxes for storing toys. They tell the computer what kind of data (e.g.,
numbers, text) a variable can hold and what operations (like adding or
comparing) can be done with it.
Q3: What is encapsulation in OOP? A: Encapsulation is like putting a toy
in a locked box. Only certain parts (like buttons to press) are accessible,
keeping the toy’s inner workings safe and organized.
Q4: Name two primitive and two non-primitive data types in Java. A:
• Primitive: int, double
• Non-Primitive: String, user-defined classes (e.g., Dog)
Q5: How do you create an object in Java? A: Use the new keyword
followed by the class name and parentheses. Example: Dog myDog = new
Dog();
Breakdown of Key Terminology
• Object-Oriented Programming (OOP): A programming style that
uses objects to represent data and behavior, making code modular
and reusable.
• Class: A template defining attributes (data) and methods (actions)
for objects.
• Object: An instance of a class with specific values for its attributes.
• Attribute: A variable inside a class that holds data (e.g., name in the
Dog class).
• Method: A function inside a class that defines an action (e.g., bark()).
• Data Type: Specifies the kind of data a variable can hold (e.g., int,
String).
• Primitive Data Type: Simple data types that store single values (e.g.,
int, double).
• Non-Primitive Data Type: Complex types that can hold multiple
values or objects (e.g., String, classes).
• Variable: A named storage location for a value, with a specific data
type.
• Instantiation: The process of creating an object from a class using
the new keyword.

You might also like