OOP-Lecture 1
OOP-Lecture 1
Unit 1
Basic Concepts of OOP and its Benefits
Object-oriented programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc. in programming. The main aim of OOPs is
to bind together the data and the functions that operate on them so that no other
part of the code can access this data except that function.
What are the OOPS Concepts in Java?
1. Objects & Classes
Objects are the basic unit of OOPS representing real-life entities. They are
invoked with the help of methods. These methods are declared within a class.
Usually, a new keyword is used to create an object of a class in Java.
Class is a predefined or user-defined template from which objects are created. It
represents properties/methods that are common to all objects of the same class. It
has several features, such as access modifiers, class names, interfaces, and class
bodies.
What are the OOPS Concepts in Java? Cont…
2. Abstraction
Abstraction means showing only the relevant details to the end-user and hiding
the irrelevant features that serve as a distraction. For example, during an ATM
operation, we only answer a series of questions to process the transaction without
any knowledge about what happens in the background between the bank and the
ATM.
What are the OOPS Concepts in Java? Cont…
3. Encapsulation
Inheritance is the process by which one class inherits the functions and properties of another class. The
main function of inheritance is the reusability of code. Each subclass only has to define its features. The
rest of the features can be derived directly from the parent class.
● Single Inheritance – Refers to a parent-child relationship where a child class extends the parent
class features. Class Y extends Class X.
● Multilevel Inheritance – Refers to a parent-child relationship where a child class extends another
child’s class. Class Y extends Class X. Class Z extends Class Y.
● Hierarchical Inheritance – This refers to a parent-child relationship where several child classes
extend one class. Class Y extends Class X, and Class Z extends Class X.
● Multiple Inheritance – Refers to a parent-child relationship where one child class is extending from
two or more parent classes. JAVA does not support this inheritance.
What are the OOPS Concepts in Java? Cont…
5. Polymorphism – Static and Dynamic
1. Simple
2. Object-Oriented
3. Platform independent
4. Portable
5. Secured
6. Robust
7. Architecture neutral
8. Interpreted
9. High Performance
10. Multithreaded
11. Distributed
12. Dynamic
Simple
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand.
According to Sun Microsystem, Java language is a simple programming language
because:
○ Java syntax is based on C++ (so easier for programmers to learn it after C++).
○ Java has removed many complicated and rarely-used features, for example, explicit
pointers, operator overloading, etc.
○ There is no need to remove unreferenced objects because there is an Automatic
Garbage Collection in Java.
Object-oriented
Java is an object-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software
as a combination of different types of objects that incorporate both data and behavior. Object-oriented programming (OOPs) is a
methodology that simplifies software development and maintenance by providing some rules.
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent
Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into
platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software
environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides a software-based platform.
The Java platform differs from most other platforms in the sense that it is a software-based platform that runs on top
of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code
is compiled by the compiler and converted into bytecode. This bytecode is a platform-independent code because it
can be run on multiple platforms, i.e., Write Once and Run Anywhere (WORA).
Portable
Java is portable because it facilitates you to carry the Java bytecode to any
platform. It doesn't require any implementation.
Secured
Java is best known for its security. With Java, we can develop virus-free systems.
Java is secured because:
● No explicit pointer
● Java Programs run inside a virtual machine sandbox
Robust
The English mining of Robust is strong. Java is robust because:
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture
and 4 bytes of memory for 64-bit architecture. However, it occupies 4 bytes of
memory for both 32 and 64-bit architectures in Java.
High-performance
Java is faster than other traditional interpreted programming languages because
Java bytecode is "close" to native code. It is still a little bit slower than a compiled
language (e.g., C++). Java is an interpreted language that is why it is slower than
compiled languages, e.g., C, C++, etc.
Distributed
Java is distributed because it facilitates users to create distributed applications in
Java. RMI and EJB are used for creating distributed applications. This feature of
Java makes us able to access files by calling the methods from any machine on
the internet.
Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java
programs that deal with many tasks at once by defining multiple threads. The main
advantage of multi-threading is that it doesn't occupy memory for each thread. It
shares a common memory area. Threads are important for multi-media, Web
applications, etc.
Dynamic
Java is a dynamic language. It supports the dynamic loading of classes. It means
classes are loaded on demand. It also supports functions from its native
languages, i.e., C and C++.
Data types & Operators in java
Data types are divided into two groups:
● Primitive data types - includes byte, short, int, long, float, double, boolean and
char
● Non-primitive data types - such as String, Arrays and Classes.
Source:
https://www.geeksfor
geeks.org/data-type
s-in-java/
char a = 'G';
int i = 89;
byte b = 4;
// byte b1 = 7888888955;
short s = 56;
double d = 4.355453532;
float f = 4.7333434f;
long l = 12121;
1. Arithmetic Operators
2. Unary Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operators
9. instance of operator
1. Arithmetic Operators
// Arithmetic operators on integers
int a = 10;
int b = 3;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
Output:
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
2. Unary Operators
int a = 10;
int b = 10;
// Using unary operators
System.out.println("Postincrement : " + (a++));
System.out.println("Preincrement : " + (++a));
System.out.println("Postdecrement : " + (b--));
System.out.println("Predecrement : " + (--b));
Output:
Postincrement : 10
Preincrement : 12
Postdecrement : 10
Predecrement : 8
3. Assignment Operator
int f = 7;
System.out.println("f += 3: " + (f += 3));
System.out.println("f -= 2: " + (f -= 2));
System.out.println("f *= 4: " + (f *= 4));
System.out.println("f /= 3: " + (f /= 3));
System.out.println("f %= 2: " + (f %= 2));
Output:
f += 3: 10
f -= 2: 8
f *= 4: 32
f /= 3: 10
f %= 2: 0
4. Relational Operators
int a = 10; Output:
boolean y = false;
Output:
x && y: false
x || y: true
!x: false
6. Ternary operator
int marks = 20;
Output:
int e = 0b1100;
Output
d & e : 8
d | e : 14
d ^ e : 6
8. Shift Operators
int a = 10;
Output
a<<1 : 20
a>>1 : 5
9. instanceof operator
class Person {}
interface MyInterface {}
Output: