0% found this document useful (0 votes)
5 views7 pages

OOP With Java Week 2

This document provides an overview of Object-Oriented Programming (OOP) with Java, detailing its principles, structure, and key components such as classes, objects, methods, and variables. It explains the main principles of OOP including encapsulation, abstraction, inheritance, and polymorphism, along with the different types of variables in Java: local, instance, and class/static variables. Additionally, it covers Java's primitive and reference data types, highlighting their characteristics and usage.

Uploaded by

gururajmeti18
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)
5 views7 pages

OOP With Java Week 2

This document provides an overview of Object-Oriented Programming (OOP) with Java, detailing its principles, structure, and key components such as classes, objects, methods, and variables. It explains the main principles of OOP including encapsulation, abstraction, inheritance, and polymorphism, along with the different types of variables in Java: local, instance, and class/static variables. Additionally, it covers Java's primitive and reference data types, highlighting their characteristics and usage.

Uploaded by

gururajmeti18
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

20CS43: OOP With Java

Object Oriented Programming and Design with Java


Week 2

2.1 What is object-oriented programming?


Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented
Programming is a methodology or paradigm to design a program using classes and objects. Object-oriented
programming (OOP) is a computer programming model that organizes software design around data, or objects,
rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.
OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate
them. This approach to programming is well-suited for programs that are large, complex and actively updated or
maintained. The organization of an object-oriented program also makes the method beneficial to collaborative
development, where projects are divided into groups. Additional benefits of OOP include code reusability,
scalability and efficiency.

2.2 Structure of object-oriented programming


The structure, or building blocks, of object-oriented programming include the following:
 Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods.
 Objects are instances of a class created with specifically defined data. Objects can correspond to real-
world objects or an abstract entity. When class is defined initially, the description is the only object that is
defined.
 Methods are functions that are defined inside a class that describe the behaviors of an object. Each method
contained in class definitions starts with a reference to an instance object. Additionally, the subroutines
contained in an object are called instance methods. Programmers use methods for reusability or keeping
functionality encapsulated inside one object at a time.
 Attributes are defined in the class template and represent the state of an object. Objects will have data
stored in the attributes field. Class attributes belong to the class itself.

2.3 What are the main principles of OOP?


Object-oriented programming is based on the following principles:
 Encapsulation. This principle states that all important information is contained inside an object and only
select information is exposed. The implementation and state of each object are privately held inside a
defined class. Other objects do not have access to this class or the authority to make changes. They are only
able to call a list of public functions or methods. This characteristic of data hiding provides greater
program security and avoids unintended data corruption.
 Abstraction. Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding
any unnecessary implementation code. The derived class can have its functionality extended. This concept
can help developers more easily make additional changes or additions over time.
 Inheritance. Classes can reuse code from other classes. Relationships and subclasses between objects can
be assigned, enabling developers to reuse common logic while still maintaining a unique hierarchy. This
property of OOP forces a more thorough data analysis, reduces development time and ensures a higher
level of accuracy.
 Polymorphism. Objects are designed to share behaviors and they can take on more than one form. The
program will determine which meaning or usage is necessary for each execution of that object from a
parent class, reducing the need to duplicate code. A child class is then created, which extends the

Shatajbegum Nadaf, SCKGPT, Bankapur Page 1


20CS43: OOP With Java

functionality of the parent class. Polymorphism allows different types of objects to pass through the same
interface.
2.4 Objects in Java
 If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these
objects have a state and a behavior.
 If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail,
running.
 If you compare the software object with a real-world object, they have very similar characteristics.
 Software objects also have a state and a behavior. A software object's state is stored in fields and behavior
is shown via methods.
 So in software development, methods operate on the internal state of an object and the object-to-object
communication is done via methods.

2.5 Classes in Java


A class is a blueprint from which individual objects are created.
Following is a sample of a class.
Example
public class Dog {
String breed;
int age;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}

2.6 Variables
A variable provides us with named storage that our programs can manipulate. Each variable in Java has a
specific type, which determines the size and layout of the variable's memory; the range of values that can be stored
within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. Following is the basic form of a variable declaration −
data type variable [ = value][, variable [ = value] ...] ;
Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one
variable of the specified type, you can use a comma-separated list.
Following are valid examples of variable declaration and initialization in Java −
Example
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = 'a'; // the char variable a iis initialized with value 'a'

There are three kinds of variables in Java −


 Local variables
 Instance variables

Shatajbegum Nadaf, SCKGPT, Bankapur Page 2


20CS43: OOP With Java

 Class/Static variables

2.6.1 Local Variables


 Local variables are declared in methods, constructors, or blocks.
 Local variables are created when the method, constructor or block is entered and the variable will be
destroyed once it exits the method, constructor, or block.
 Access modifiers cannot be used for local variables.
 Local variables are visible only within the declared method, constructor, or block.
 Local variables are implemented at stack level internally.
 There is no default value for local variables, so local variables should be declared and an initial value
should be assigned before the first use.
Example
Here, age is a local variable. This is defined inside pupAge() method and its scope is limited to only this method.
public class Test {
public void pupAge() {
int age = 0;
age = age + 7;
[Link]("Puppy age is : " + age);
}

public static void main(String args[]) {


Test test = new Test();
[Link]();
}
}
This will produce the following result −
Output
Puppy age is: 7

Example
Following example uses age without initializing it, so it would give an error at the time of compilation.
public class Test {
public void pupAge() {
int age;
age = age + 7;
[Link]("Puppy age is : " + age);
}
public static void main(String args[]) {
Test test = new Test();
[Link]();
}
}
This will produce the following error while compiling it −
Output
[Link]:variable number might not have been initialized
age = age + 7;
^
1 error
Shatajbegum Nadaf, SCKGPT, Bankapur Page 3
20CS43: OOP With Java

2.6.2 Instance Variables


 Instance variables are declared in a class, but outside a method, constructor or any block.
 When a space is allocated for an object in the heap, a slot for each instance variable value is created.
 Instance variables are created when an object is created with the use of the keyword 'new' and destroyed
when the object is destroyed.
 Access modifiers can be given for instance variables.
 The instance variables are visible for all methods, constructors and block in the class. Normally, it is
recommended to make these variables private (access level). However, visibility for subclasses can be
given for these variables with the use of access modifiers.
 Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for
object references it is null. Values can be assigned during the declaration or within the constructor.
 Instance variables can be accessed directly by calling the variable name inside the class. However, to
access them outside a class we need to create an object of the class to which it belongs.
Example
public class Employee
{
String name;
double salary;

public Employee (String empName) {


name = empName;
}

public void setSalary(double empSal) {


salary = empSal;
}

public void printEmp() {


[Link]("name : " + name );
[Link]("salary :" + salary);
}

public static void main(String args[]) {


Employee emp = new Employee("Shataj");
[Link](100000);
[Link]();
}
}
This will produce the following result −
Output
name : Shataj
salary :100000.0

2.6.3 Class/Static Variables


 Class variables also known as static variables are declared with the static keyword in a class, but outside a
method, constructor or a block.
 There would only be one copy of each class variable per class, regardless of how many objects are created
from it.

Shatajbegum Nadaf, SCKGPT, Bankapur Page 4


20CS43: OOP With Java

 Visibility is similar to instance variables. However, most static variables are declared public since they
must be available for users of the class.
 Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false;
and for object references, it is null. Values can be assigned during the declaration or within the constructor.
 We need not create any object to access static variables. Static variables can be accessed by calling with the
class name [Link].
 When declaring class variables as public static final, then variable names (constants) are all in upper case.
If the static variables are not public and final, the naming syntax is the same as instance and local variables.
Example
import [Link].*;
public class Employee {
static double salary;
// DEPARTMENT is a constant
public static final String DEPARTMENT = "Development ";

}
class EmpTest
{
public static void main(String args[]) {
salary = 1000;
[Link](DEPARTMENT + "average salary:" + salary);
}
}
This will produce the following result −
Output
Development average salary:1000

2.7 Datatypes
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the
reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or
characters in these variables.
There are two data types available in Java −
 Primitive Data Types
 Reference/Object Data Types

2.7.1 Primitive Data Types


There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and
named by a keyword. Let us now look into the eight primitive data types in detail.
byte
 Byte data type is an 8-bit signed two's complement integer
 Minimum value is -128 (-2^7)
 Maximum value is 127 (inclusive)(2^7 -1)
 Default value is 0
 Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times
smaller than an integer.
 Example: byte a = 100, byte b = -50

Shatajbegum Nadaf, SCKGPT, Bankapur Page 5


20CS43: OOP With Java

short
 Short data type is a 16-bit signed two's complement integer
 Minimum value is -32,768 (-2^15)
 Maximum value is 32,767 (inclusive) (2^15 -1)
 Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an
integer
 Default value is 0.
 Example: short s = 10000, short r = -20000

int
 Int data type is a 32-bit signed two's complement integer.
 Minimum value is - 2,147,483,648 (-2^31)
 Maximum value is 2,147,483,647(inclusive) (2^31 -1)
 Integer is generally used as the default data type for integral values unless there is a concern about
memory.
 The default value is 0
 Example: int a = 100000, int b = -200000

long
 Long data type is a 64-bit signed two's complement integer
 Minimum value is -9,223,372,036,854,775,808(-2^63)
 Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
 This type is used when a wider range than int is needed
 Default value is 0L
 Example: long a = 100000L, long b = -200000L

float
 Float data type is a single-precision 32-bit IEEE 754 floating point
 Float is mainly used to save memory in large arrays of floating point numbers
 Default value is 0.0f
 Float data type is never used for precise values such as currency
 Example: float f1 = 234.5f

double
 double data type is a double-precision 64-bit IEEE 754 floating point
 This data type is generally used as the default data type for decimal values, generally the default choice
 Double data type should never be used for precise values such as currency
 Default value is 0.0d
 Example: double d1 = 123.4

boolean
 boolean data type represents one bit of information
 There are only two possible values: true and false
 This data type is used for simple flags that track true/false conditions
 Default value is false

Shatajbegum Nadaf, SCKGPT, Bankapur Page 6


20CS43: OOP With Java

 Example: boolean one = true

char
 char data type is a single 16-bit Unicode character
 Minimum value is '\u0000' (or 0)
 Maximum value is '\uffff' (or 65,535 inclusive)
 Char data type is used to store any character
 Example: char letterA = 'A'

2.7.2 Reference Datatypes


 Reference variables are created using defined constructors of the classes. They are used to access objects.
These variables are declared to be of a specific type that cannot be changed. For example, Employee,
Puppy, etc.
 Class objects and various type of array variables come under reference datatype.
 Default value of any reference variable is null.
 A reference variable can be used to refer any object of the declared type or any compatible type.
 Example: Animal animal = new Animal("giraffe");

Shatajbegum Nadaf, SCKGPT, Bankapur Page 7

You might also like