oblect oriented programming language in java notes .pdf

sanraku980 12 views 21 slides Mar 12, 2025
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

object oriented programming language in java concepts like what are objects and classes


Slide Content

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which contain both data (attributes) and
methods (functions) that operate on the data. OOP focuses on modeling real-world entities and promotes code reusability, modularity, and
scalability.

Key Principles of OOP

Encapsulation – Bundling data and methods together to restrict direct access to data.1.
Abstraction – Hiding complex implementation details and exposing only the necessary functionalities.2.
Inheritance – Allowing one class to derive properties and behaviors from another, promoting reusability.3.
Polymorphism – Enabling a single function or method to behave differently based on context or input.4.

Key Features of OOP

Classes & Objects: A class is a blueprint, and objects are instances of that blueprint.
Modularity: Code is organized into self-contained objects.
Code Reusability: Through inheritance and modular design.

Examples of OOP Languages

Java
C++
Python
C#
Ruby

OOP is widely used in software development because it helps create maintainable, scalable, and reusable code.

 

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

Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for
the application, and place them at a single place and reuse them instead of repeating it.

Data types are divided into two groups:

Primitive data types - includes byte, short, int, long, Noat, double, boolean and char
Son-primitive data types - such as 8tring, Arrays and Classes


Primitive Data Types

A primitive data type speci7es the type of a variable and the kind of values it can hold.

There are eight primitive data types in Java:

Data TypeDescription
byte 8tores whole numbers from -126 to 129
short 8tores whole numbers from -32,906 to 32,909
int 8tores whole numbers from -2,149,463,046 to 2,149,463,049
long 8tores whole numbers from -5,223,392,/30,6I4,99I,6/6 to 5,223,392,/30,6I4,99I,6/9
Noat 8tores fractional numbers. 8uMcient for storing 0 to 9 decimal digits
double 8tores fractional numbers. 8uMcient for storing 1I to 10 decimal digits
boolean8tores true or false values
char 8tores a single characterWletter or A8C?? values


 

Non-Primitive Data Types

Son-primitive data types are called reference types because they refer to objects.

The main differences between primitive and non-primitive data types are:

Primitive types in Java are prede7ned and built into the language, while non-primitive types are created by the programmer (except
for 8tring).
Son-primitive types can be used to call methods to perform certain operations, whereas primitive types cannot.
Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like 8tring).
Primitive types always hold a value, whereas non-primitive types can be null.

Examples of non-primitive types are 8trings, Arrays, Classes etc.

 


A method is a block of code which only runs when it is called.

You can pass data, known as parameters, into a method.

{ethods are used to perform certain actions, and they are also known as functions.

}hy use methods! To reuse code: de7ne the code once, and use it many times.


Create a Method

A method must be declared within a class. ?t is de7ned with the name of the method, followed by parentheses (). Java provides some pre-
de7ned methods, such as 8ystem.out.println(), but you can also create your own methods to perform certain actions:

Example

Create a method inside {ain:

public class {ain ;
static void my{ethod() ;
WW code to be executed
[
[
Example Explained


my{ethod() is the name of the method
static means that the method belongs to the {ain class and not an object of the {ain class. You will learn more about objects and how to
access methods through objects later in this tutorial.
void means that this method does not have a return value. You will learn more about return values later in this chapter


Call a Method

To call a method in Java, write the method's name followed by two parentheses () and a semicolon;

?n the following example, my{ethod() is used to print a text (the action), when it is calledExample

?nside main, call the my{ethod() method:

public class {ain ;
static void my{ethod() ;
8ystem.out.println("? just got executed]")L
[
public static void main(8tring<q args) ;
my{ethod()L
[
[
WW Outputs "? just got executed]"

 


A method can also be called multiple times:


Example

public class {ain ;
static void my{ethod() ;
8ystem.out.println("? just got executed]")L
[
public static void main(8tring<q args) ;
my{ethod()L
my{ethod()L

my{ethod()L
[
[
WW ? just got executed]
WW ? just got executed]
WW ? just got executed]

Parameters and Arguments

 

?nformation can be passed to methods as a parameter. Parameters act as variables inside the method.

 

Parameters are speci7ed after the method name, inside the parentheses. You can add as many parameters as you want, just separate them
with a comma.

 

The following example has a method that takes a 8tring called fname as parameter. }hen the method is called, we pass along a 7rst name,
which is used inside the method to print the full name:

 

Example

public class {ain ;
static void my{ethod(8tring fname) ;
8ystem.out.println(fname + " Refsnes")L
[
public static void main(8tring<q args) ;
my{ethod("Fiam")L
my{ethod("Jenny")L
my{ethod("Anja")L
[
[
WW Fiam Refsnes
WW Jenny Refsnes
WW Anja Refsnes

 

 



}hen a parameter is passed to the method, it is called an argument. 8o, from the example above: fname is a parameter,
while Fiam, Jenny and Anja are arguments.


 

 

Multiple Parameters

 

You can have as many parameters as you like:

 


Example

public class {ain ;
static void my{ethod(8tring fname, int age) ;
8ystem.out.println(fname + " is " + age)L
[
public static void main(8tring<q args) ;
my{ethod("Fiam", I)L
my{ethod("Jenny", 6)L
my{ethod("Anja", 31)L
[
[
WW Fiam is I
WW Jenny is 6
WW Anja is 31

 

 


Sote that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters,
and the arguments must be passed in the same order.


 
A {ethod with ?f...Else
 

?t is common to use if...else statements inside methods:

 


Example

public class {ain ;
WW Create a checkAge() method with an integer variable called age
static void checkAge(int age) ;
WW ?f age is less than 16, print "access denied"
if (age = 16) ;
8ystem.out.println("Access denied - You are not old enough]")L
WW ?f age is greater than, or e\ual to, 16, print "access granted"

[ else ;
8ystem.out.println("Access granted - You are old enough]")L
[
[
public static void main(8tring<q args) ;
checkAge(2/)L WW Call the checkAge method and pass along an age of 2/
[
[
WW Outputs "Access granted - You are old enough]"

Java Classes/Objects

 

Java is an object-oriented programming language.

 

Everything in Java is associated with classes and objects, along with its attributes and methods. Uor 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.

 

 

Create a Class

 

To create a class, use the keyword class:

 


Main.java

Create a class named "{ain" with a variable x:

public class {ain ;
int x > IL
[


 


Remember from the Java 8yntax chapter that a class should always start with an uppercase 7rst letter, and that the name of the java 7le should
match the class name.

 


 

Create an Object

 

?n Java, an object is created from a class. }e have already created the class named {ain, so now we can use this to create objects.

 

To create an object of {ain, specify the class name, followed by the object name, and use the keyword new:Example

 


Create an object called "myObj" and print the value of x:

public class {ain ;
int x > IL
public static void main(8tring<q args) ;
{ain myObj > new {ain()L

8ystem.out.println(myObj.x)L
[
[

 


 


 




 


 

Multiple Objects

 

You can create multiple objects of one class:Example

 

Create two objects of {ain:

public class {ain ;
int x > IL
public static void main(8tring<q args) ;
{ain myObj1 > new {ain()L WW Object 1
{ain myObj2 > new {ain()L WW Object 2
8ystem.out.println(myObj1.x)L
8ystem.out.println(myObj2.x)L
[
[

 

 

 


 


 

Using Multiple Classes

 

You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all
the attributes and methods, while the other class holds the main() method (code to be executed)).

 

Remember that the name of the java 7le should match the class name. ?n this example, we have created two 7les in the same directoryWfolder:

 

{ain.java
8econd.java

 


Main.java

public class {ain ;
int x > IL
[


 


Second.java

class 8econd ;

public static void main(8tring<q args) ;
{ain myObj > new {ain()L
8ystem.out.println(myObj.x)L
[
[


 

}hen both 7les have been compiled:

 


C:»Ssers»Your NameTjavac {ain.java
C:»Ssers»Your NameTjavac 8econd.java


 

Run the 8econd.java 7le:

 


C:»Ssers»Your NameTjava 8econd


 

And the output will be:

 


I


Modi=ers

By now, you are \uite familiar with the public keyword that appears in almost all of our examples:

public class {ain


The public keyword is an access modi=er, meaning that it is used to set the access level for classes, attributes, methods and constructors.

}e divide modi7ers into two groups:

Access Modi=ers - controls the access level
Non-Access Modi=ers - do not control access level, but provides other functionality


Access Modi=ers

Uor classes, you can use either public or default:

Modi=erDescription Try it
publicThe class is accessible by any other class Try it
U
defaultThe class is only accessible by classes in the same package. This is used when you don't specify a modi7er. You will learn
more about packages in the Packages chapter
Try it
U


Uor attributes, methods and constructors, you can use the one of the following:


Modi=erDescription Try it
public The code is accessible for all classes Try it
U
privateThe code is only accessible within the declared class Try it
U
defaultThe code is only accessible in the same package. This is used when you don't specify a modi7er. You will learn more about
packages in the Packages chapter
Try it
U
protectedThe code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in
the ?nheritance chapter
Try it
U



Non-Access Modi=ers

Uor classes, you can use either 7nal or abstract:


Modi=erDescription Try it
7nal The class cannot be inherited by other classes (You will learn more about inheritance in the ?nheritance chapter) Try it
U
abstractThe class cannot be used to create objects (To access an abstract class, it must be inherited from another class. You will
learn more about inheritance and abstraction in the ?nheritance and Abstraction chapters)
Try it
U


Uor attributes and methods, you can use the one of the following:


Modi=er Description
7nal Attributes and methods cannot be overriddenWmodi7ed
static Attributes and methods belongs to the class, rather than an object
abstract Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for
example abstract void run();. The body is provided by the subclass (inherited from). You will learn more about inheritance and
abstraction in the ?nheritance and Abstraction chapters
transient Attributes and methods are skipped when serializing the object containing them
synchronized{ethods can only be accessed by one thread at a time
volatile The value of an attribute is not cached thread-locally, and is always read from the "main memory"

Final

?f you don't want the ability to override existing attribute values, declare attributes as 7nal:


Example

=nal int x 0 13; =nal double PI 0 4.1[; public static void main(String]{ args) 5 Main
myObj 0 new Main(); myObj.x 0 }3; // will generate an error: cannot assign a value to
a =nal variable myObj.PI 0 2}; // will generate an error: cannot assign a value to a
=nal variable System.out.println(myObj.x); 5 5