A Lecture Number Oop1 (Java)
A Lecture Number Oop1 (Java)
• 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
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.
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
Syntax to declare a class:
1. class <class_name>{
2. field;
3. method;
4. }
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
2. Advantage of Method
• Code Reusability
• Code Optimization
The new keyword is used to allocate memory at runtime. All objects get
memory in Heap memory area.
Output:
0
null
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
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
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