0% found this document useful (0 votes)
12 views10 pages

INTRO TO OOP - 2nd2023 - 24

Uploaded by

crapaaud
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)
12 views10 pages

INTRO TO OOP - 2nd2023 - 24

Uploaded by

crapaaud
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

CLASSES AND OBJECTS

Self-paced activity:
Java OOPs Concepts
a. https://www.javatpoint.com/java-oops-concepts
b. https://youtu.be/pTB0EiLXUC8
c. https://youtu.be/m_MQYyJpIjg

Object-Oriented Programming (OOP)


➢ OOP involves the creation of classes by modelling the real world.
➢ The first step in problem-solving using object-oriented design (OOD) is to identify the
components called classes and objects.
➢ Class allows you to combine data and operations on a single unit.
➢ Combining data and operation is called encapsulation (the first principle of OOD)

Classes and Objects


➢ Objects are defined by classes.
➢ An object in a class has both data and operations(methods) that can be performed on
that data.
➢ Objects created from the same class share the same definition of attributes
and methods but their state may differ.
Example:

➢ Objects from different classes do not share the same definition of attributes
or methods.
Example:
Class vs. Object

A Class consists of :
1. A unique name (starts with an uppercase letter)
2. A list of attributes (int, double, Boolean, String, etc)
- attributes are the variables you define when creating a class
3. A list of methods
This is shown in a simple box structure:

Unified Modeling Language(UML) Class Diagrams


➢ A class and its members or instance members(attributes and methods ) can be
described graphically using UML notation
Examples:
Class Definition

Instance variable
➢ It is a variable defined within a class, each instance of the class (that is, each object of
the class) contains its own copy of these variables.

Accessing members of the class


➢ You can access the members of the class by creating an object of the class, and by
using the dot operator (.)
Sample Code:
public class Human
{
String name = "John";
int age = 20;

public void greet()


{
System.out.println("Welcome to OOP " +name);
}

public static void main(String[] args)


{
Human p1 = new Human();
Human p2 = new Human();
System.out.print("Name : "+p1.name);
System.out.println("\nAge : "+p1.age);
System.out.print("Name : "+p2.name);
System.out.println("\nAge : "+p2.age);
p2.name = "Jean";
p2.age = 21;
System.out.print("Name : "+p2.name);
System.out.println("\nAge : "+p2.age);
p1.greet();
p2.greet();
System.out.print("\n");
}
}

Multiple Objects
➢ If you create multiple objects of one class, you can change the attribute values in
one object, without affecting the attribute values in the other.

Constructors
➢ You use a constructor to give initial values to the instance variables defined by
the class.
➢ All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member variables
to zero. However, once you define your own constructor, the default constructor
is no longer used.

Rules for creating Java constructor


1. Constructor name must be the same as its class name.
2. A constructor must have no explicit return type.
3. A Java constructor cannot be abstract, static, final, and synchronized.

Types of Java constructors


1. Default constructor (no-argument constructor)
- it doesn't have any parameter.
- used to provide the default values to the object like 0, null, etc., depending on the
type.

2. Parameterized constructor
- A constructor which has a specific number of parameters
- used to provide different values to distinct objects.
Code: OOPTriangle

Encapsulation
➢ Encapsulation is one of the four fundamental OOP concepts.
➢ a process of wrapping code and data together into a single unit
➢ the variables of a class will be hidden from other classes, and can be accessed only
through the methods of their current class (also known as data hiding)

To achieve encapsulation in Java:


1. Declare the variables of a class as private.
2. Provide public setter and getter methods to modify and view the variables values.

Getter and Setter in Java


➢ Two conventional methods used to retrieve and update values of a variable.
➢ They are mainly used to create, modify, delete, and view the variable values.

The setter method


➢ A public method used for updating values; also known as an mutator.
The getter method
➢ A public method used for reading or retrieving the values; also known as accessor.

The following code is an example of getter and setter methods:

Benefits of Encapsulation
➢ The fields of a class can be made read-only or write-only.
➢ A class can have total control over what is stored in its fields.
Code: OOPTriangle

Sample Code: OOPRectangle


Sample Problem:
Write a simple calculator program with the following functionalities: +, -, *, /, ab, a1/b. Use 2
operands (a and b) for the arithmetic operations. Use OOP approach and make sure to apply
encapsulation.
Filename: OOPCalculator

Java Package
➢ A group of similar types of classes, interfaces and sub-packages.

Package in java can be categorized in two forms :


1. Built-in package

3. User-defined package.

Java Source File Structure


➢ A Java program has the following structure:

1. package statements
A package in Java is a mechanism to encapsulate a group of classes, sub-packages,
and interfaces.
(Imports and packages : https://youtu.be/ipMdsje9J6s)

2. import statements
The import statement is used to import a package, class, or interface.
3. class definitions

Important Points to keep in mind while working with Java Source File
1. A Java program can contain any number of classes, and at most, one of them can
be declared as a public class.
2. The name of the Java source file can be anything provided that no class is declared as
public.
Code: MultiClass

Access Modifiers in Java


➢ Sets access levels for classes, variables, methods, and constructors.

The four access levels are:


1. default – no modifiers needed
2. private - accessible only within the class
3. public - accessible everywhere
4. protected - accessible within package and outside the package but through
inheritance only. It can be applied on the data member, method and constructor.
Access modifiers in Java

Sample Codes : Private Modifier, ModifierDefault

The Java static keyword


➢ It is mainly used for memory management.
➢ It can be applied to class variables, class methods, blocks and nested classes.
➢ It belongs to the class than an instance of the class.
Java static variable
➢ Any variable declared as static is known as a static variable.
➢ Can be used to refer to the common property of all objects (company name of
employees, college name of students, etc.
➢ The static variable gets memory only once in the class area at the time of class
loading.

Sample Program : StaticVariable

class Student
{
int ID;
String name;
static String college ="ITS";

Student(int r, String n)
{
ID = r;
name = n;
}

void display ()
{
System.out.println(ID+" "+name+" "+college);}
}

public class StaticVariable


{
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();

}
}
What is the output of this program?

class Counter
{
int count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}

What is the output of the program below?


class Counter2
{
static int count=0;
Counter2()
{
count++;
System.out.println(count);
}

public static void main(String args[])


{
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
Source code : StaticVariableCounter

Java static method


➢ It belongs to the class rather than the object of a class.
➢ It can be invoked without the need for creating an instance of a class.
➢ It can access static data member and can change the value of it.

Sample Program:

class Calculate
{
static int cube(int x)
{
return x*x*x;
}
public static void main(String args[])
{
int result=cube(5);
System.out.println(result);
System.out.println(Calculate.sum(2,3));
}
static int sum(int x, int y)
{ return x+y; }
}

You might also like