Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

A Lecture Number Oop1 (Java)

information about OOP

Uploaded by

Itachi Utcheha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

A Lecture Number Oop1 (Java)

information about OOP

Uploaded by

Itachi Utcheha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

OOP (java)

MSc. Mohammed AL-Sayani


Java OOPs Concepts
1. Object-Oriented Programming
2. Advantage of OOPs over Procedure-
oriented programming language
3. Difference between Object-oriented and
Object-based programming language.
In this page, we will learn about the basics of OOPs.
Object-Oriented Programming is a paradigm that
provides many concepts, such as inheritance, data
binding, polymorphism, etc.
Simula is considered the first object-oriented
programming language. The programming paradigm
where everything is represented as an object is known as
a truly object-oriented programming language.
Smalltalk is considered the first truly object-oriented
programming language.
The popular object-oriented languages are Java, C#,
PHP, Python, C++, etc.
The main aim of object-oriented programming is to
implement real-world entities, for example, object,
classes, abstraction, inheritance, polymorphism, etc.

OOPs (Object-Oriented Programming


System)
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. It simplifies software
development and maintenance by providing some
concepts:

• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

Object
Any entity that has state and behavior is known as an
object. For example, a chair, pen, table, keyboard, bike,
etc. It can be physical or logical.
An Object can be defined as an instance of a class. An
object contains an address and takes up some space in
memory. Objects can communicate without knowing the
details of each other's data or code. The only necessary
thing is the type of message accepted and the type of
response returned by the objects.
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.
A class can also be defined as a blueprint from which
you can create an individual object. Class doesn't
consume any space.
Inheritance
When one object acquires all the properties and
behaviors of a parent object, it is known as inheritance.
It provides code reusability. It is used to achieve runtime
polymorphism.

Polymorphism
If one task is performed in different ways, it is known as
polymorphism. For example: to convince the customer
differently, to draw something, for example, shape,
triangle, rectangle, etc.
In Java, we use method overloading and method
overriding to achieve polymorphism.
Another example can be to speak something; for
example, a cat speaks meow, dog barks woof, etc.

1.Abstraction

Hiding internal details and showing functionality is


known as abstraction. For example phone call, we don't
know the internal processing.
In Java, we use abstract class and interface to achieve
abstraction.

Encapsulation
Binding (or wrapping) code and data together into a
single unit are known as encapsulation. For example, a
capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is
the fully encapsulated class because all the data members
are private here.

Coupling
Coupling refers to the knowledge or information or
dependency of another class. It arises when classes are
aware of each other. If a class has the details information
of another class, there is strong coupling. In Java, we use
private, protected, and public modifiers to display the
visibility level of a class, method, and field. You can use
interfaces for the weaker coupling because there is no
concrete implementation.
Cohesion
Cohesion refers to the level of a component which
performs a single well-defined task. A single well-
defined task is done by a highly cohesive method. The
weakly cohesive method will split the task into separate
parts. The java.io package is a highly cohesive package
because it has I/O related classes and interface. However,
the java.util package is a weakly cohesive package
because it has unrelated classes and interfaces.

Association
Association represents the relationship between the
objects. Here, one object can be associated with one
object or many objects. There can be four types of
association between the objects:

• One to One
• One to Many
• Many to One, and
• Many to Many
Let's understand the relationship with real-time
examples. For example, One country can have one prime
minister (one to one), and a prime minister can have
many ministers (one to many). Also, many MP's can
have one prime minister (many to one), and many
ministers can have many departments (many to many).
Aggregation
Aggregation is a way to achieve Association.
Aggregation represents the relationship where one object
contains other objects as a part of its state. It represents
the weak relationship between objects. It is also termed
as a has-a relationship in Java. Like, inheritance
represents the is-a relationship. It is another way to reuse
objects.
Composition
The composition is also a way to achieve Association.
The composition represents the relationship where one
object contains other objects as a part of its state. There
is a strong relationship between the containing object and
the dependent object. It is the state where containing
objects do not have an independent existence. If you
delete the parent object, all the child objects will be
deleted automatically.

Advantage of OOPs over Procedure-


oriented programming language
1) OOPs makes development and maintenance easier,
whereas, in a procedure-oriented programming language,
it is not easy to manage if code grows as project size
increases.
2) OOPs provides data hiding, whereas, in a procedure-
oriented programming language, global data can be
accessed from anywhere.
ADVERTISEMENT
Figure: Data Representation in Procedure-Oriented
Programming

Figure: Data Representation in Object-Oriented


Programming
3) OOPs provides the ability to simulate real-world event
much more effectively. We can provide the solution of
real word problem if we are using the Object-Oriented
Programming language.
Objects and Classes in Java
1. Object in Java
2. Class in Java
3. Instance Variable in Java
4. Method in Java
5. Example of Object and class that maintains the
records of student
In this page, we will learn about Java objects and classes. In object-
oriented programming technique, we design a program using objects and
classes.

An object in Java is the physical as well as a logical entity, whereas, a


class in Java is a logical entity only.

What is an object in Java


An entity that has state and behavior is known as an object e.g., chair,
bike, marker, pen, table, car, etc. It can be physical or logical (tangible
and intangible). The example of an intangible object is the banking
system.

• State: represents the data (value) of an object.


• Behavior: represents the behavior (functionality) of an object such
as deposit, withdraw, etc.
• Identity: An object identity is typically implemented via a unique
ID. The value of the ID is not visible to the external user. However,
it is used internally by the JVM to identify each object uniquely.

For Example, Pen is an object. Its name is Reynolds; color is white,


known as its state. It is used to write, so writing is its behavior.

An object is an instance of a class. A class is a template or blueprint


from which objects are created. So, an object is the instance(result) of a
class.
What is a class in Java
A class is a group of objects which have common properties. It is a
template or blueprint from which objects are created. It is a logical entity.
It can't be physical.

A class in Java can contain:

• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
Syntax to declare a class:

1. class <class_name>{
2. field;
3. method;
4. }

Instance variable in Java

A variable which is created inside the class but outside the method is
known as an instance variable. Instance variable doesn't get memory at
compile time. It gets memory at runtime when an object or instance is
created. That is why it is known as an instance variable.

Method in Java

In Java, a method is like a function which is used to expose the behavior


of an object.

2. Advantage of Method

• Code Reusability
• Code Optimization

new keyword in Java

The new keyword is used to allocate memory at runtime. All objects get
memory in Heap memory area.

Object and Class Example: main within the class


In this example, we have created a Student class which
has two data members id and name. We are creating the
object of the Student class by new keyword and printing
the object's value.
Here, we are creating a main() method inside the class.
File: Student.java
1. //Java Program to illustrate how to define a class an
d fields
2. //Defining a Student class.
3. class Student{
4. //defining fields
5. String name;
6. int id;
7. //creating main method inside the Student class
8. public static void main(String args[]){
9. //Creating an object or instance
10. Student s1=new Student();
11. //Printing values of the object
12. System.out.println(s1.id);
13. System.out.println(s1.name);
14. }
15. }

Output:
0
null

Object and Class Example: main outside the class

In real time development, we create classes and use it from another class.
It is a better approach than previous one. Let's see a simple example,
where we are having main() method in another class.

We can have multiple classes in different Java files or single Java file. If
you define multiple classes in a single Java source file, it is a good idea to
save the file name with the class name which has main() method.

File: TestStudent1.java
1. //Java Program to demonstrate having the main met
hod in
2. //another class
3. //Creating Student class.
4. class Student{
5. int id;
6. String name;
7. }
8. class TestStudent1{
9. public static void main(String args[]){
10. Student s1=new Student();
11. System.out.println(s1.id);
12. System.out.println(s1.name);
13. }
14. }
Output:
0
null

3 Ways to initialize object


There are 3 ways to initialize object in Java.

1. By reference variable
2. By method
3. By constructor
1) Object and Class Example: Initialization through reference
Initializing an object means storing data into the object. Let's see a simple
example where we are going to initialize the object through a reference
variable.

File: TestStudent2.java

1. class Student{
2. int id;
3. String name;
4. }
5. class TestStudent2{
6. public static void main(String args[]){
7. Student s1=new Student();
8. s1.id=101;
9. s1.name="Sonoo";
10. System.out.println(s1.id+" "+s1.name);
11. }
12. }
Output:
101 Sonoo

We can also create multiple objects and store information in it through


reference variable.

File: TestStudent3.java

1. class Student{
2. int id;
3. String name;
4. }
5. class TestStudent3{
6. public static void main(String args[]){
7. //Creating objects
8. Student s1=new Student();
9. Student s2=new Student();
10. //Initializing objects
11. s1.id=101;
12. s1.name="Sonoo";
13. s2.id=102;
14. s2.name="Amit";
15. //Printing data
16. System.out.println(s1.id+" "+s1.name);
17. System.out.println(s2.id+" "+s2.name);
18. }
19. }
Object and Class Example: Initialization through method
In this example, we are creating the two objects of
Student class and initializing the value to these objects
by invoking the insertRecord method. Here, we are
displaying the state (data) of the objects by invoking the
displayInformation() method.
File: TestStudent4.java
1. class Student{
2. int rollno;
3. String name;
4. void insertRecord(int r, String n){
5. rollno=r;
6. name=n;
7. }
8. void displayInformation(){System.out.println(rolln
o+" "+name);}
9. }
10. class TestStudent4{
11. public static void main(String args[]){
12. Student s1=new Student();
13. Student s2=new Student();
14. s1.insertRecord(111,"Karan");
15. s2.insertRecord(222,"Aryan");
16. s1.displayInformation();
17. s2.displayInformation();
18. }
19. }
Output:
111 Karan
222 Aryan

As you can see in the above figure, object gets the


memory in heap memory area. The reference variable
refers to the object allocated in the heap memory area.
Here, s1 and s2 both are reference variables that refer to
the objects allocated in memory.

3) Object and Class Example: Initialization through a


constructor
We will learn about constructors in Java later.
Object and Class Example: Employee
Let's see an example where we are maintaining records
of employees.
File: TestEmployee.java
1. class Employee{
2. int id;
3. String name;
4. float salary;
5. void insert(int i, String n, float s) {
6. id=i;
7. name=n;
8. salary=s;
9. }
10. void display(){System.out.println(id+" "+na
me+" "+salary);}
11. }
12. public class TestEmployee {
13. public static void main(String[] args) {
14. Employee e1=new Employee();
15. Employee e2=new Employee();
16. Employee e3=new Employee();
17. e1.insert(101,"ajeet",45000);
18. e2.insert(102,"irfan",25000);
19. e3.insert(103,"nakul",55000);
20. e1.display();
21. e2.display();
22. e3.display();
23. }
24. }
Output:
101 ajeet 45000.0
102 irfan 25000.0
103 nakul 55000.0
Object and Class Example: Rectangle
There is given another example that maintains the
records of Rectangle class.
File: TestRectangle1.java
1. class Rectangle{
2. int length;
3. int width;
4. void insert(int l, int w){
5. length=l;
6. width=w;
7. }
8. void calculateArea(){System.out.println(length*wi
dth);}
9. }
10. class TestRectangle1{
11. public static void main(String args[]){
12. Rectangle r1=new Rectangle();
13. Rectangle r2=new Rectangle();
14. r1.insert(11,5);
15. r2.insert(3,15);
16. r1.calculateArea();
17. r2.calculateArea();
18. }
19. }
Output:
55
45
Real World Example: Account
File: TestAccount.java
1. //Java Program to demonstrate the working of a ban
king-system
2. //where we deposit and withdraw amount from our a
ccount.
3. //Creating an Account class which has deposit() and
withdraw() methods
4. class Account{
5. int acc_no;
6. String name;
7. float amount;
8. //Method to initialize object
9. void insert(int a,String n,float amt){
10. acc_no=a;
11. name=n;
12. amount=amt;
13. }
14. //deposit method
15. void deposit(float amt){
16. amount=amount+amt;
17. System.out.println(amt+" deposited");
18. }
19. //withdraw method
20. void withdraw(float amt){
21. if(amount<amt){
22. System.out.println("Insufficient Balance");
23. }else{
24. amount=amount-amt;
25. System.out.println(amt+" withdrawn");
26. }
27. }
28. //method to check the balance of the account
29. void checkBalance(){
30. System.out.println("Balance is: "+amount);}
31. //method to display the values of an object
32. void display(){
33. System.out.println(acc_no+" "+name+" "+amo
unt);}
34. }
35. //Creating a test class to deposit and withdraw
amount
36. class TestAccount{
37. public static void main(String[] args){
38. Account a1=new Account();
39. a1.insert(832345,"Ankit",1000);
40. a1.display();
41. a1.checkBalance();
42. a1.deposit(40000);
43. a1.checkBalance();
44. a1.withdraw(15000);
45. a1.checkBalance();
46. }}
Output:
832345 Ankit 1000.0
Balance is: 1000.0
40000.0 deposited
Balance is: 41000.0
15000.0 withdrawn
Balance is: 26000.0

You might also like