0% found this document useful (0 votes)
12 views

File 4 P-2 (Java OOPs Concepts)

Uploaded by

Riduan Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

File 4 P-2 (Java OOPs Concepts)

Uploaded by

Riduan Aziz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Apart from these concepts, there are some other terms which are used in Object-Oriented
design:
1. Coupling
2. Cohesion
3. Association
4. Aggregation
5. Composition

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.
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.

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).
Association can be unidirectional or bidirectional.

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.

Java Naming Convention


Java naming convention is a rule to follow as you decide what to name your identifiers such
as class, package, variable, constant, method, etc.
Identifiers Naming Rules Examples
Type
Class It should start with the uppercase letter. public class Employee
It should be a noun such as Color, Button, {
System, Thread, etc. //code snippet
Use appropriate words, instead of acronyms. }
Interface It should start with the uppercase letter. interface Printable
It should be an adjective such as Runnable, {
Remote, Action Listener. //code snippet
Use appropriate words, instead of acronyms. }
Method It should start with lowercase letter. class Employee
It should be a verb such as main(), print(), {
println(). // method
If the name contains multiple words, start it void draw()
with a lowercase letter followed by an {
uppercase letter such as actionPerformed(). //code snippet
}}
Variable It should start with a lowercase letter such as class Employee
id, name. {
It should not start with the special characters // variable
like & (ampersand), $ (dollar), _ (underscore). int id;
If the name contains multiple words, start it //code snippet
with the lowercase letter followed by an }
uppercase letter such as firstName, lastName.
Avoid using one-character variables such as x,
y, z.
Package It should be a lowercase letter such as java, //package
lang. package com.jhtitow;
If the name contains multiple words, it should class Employee
be separated by dots (.) such as java.util, {
java.lang. //code snippet
}
Constant It should be in uppercase letters such as RED, class Employee
YELLOW. {
If the name contains multiple words, it should //constant
be separated by an underscore(_) such as static final int MIN_AGE = 18;
MAX_PRIORITY. //code snippet
It may contain digits but not as the first letter. }

CamelCase in Java naming conventions


Java follows camel-case syntax for naming the class, interface, method, and variable.
If the name is combined with two words, the second word will start with uppercase letter
always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.

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.
An object has three characteristics:

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.
Object Definitions:
1. An object is a real-world entity.
2. An object is a runtime entity.
3. The object is an entity which has state and behavior.
4. The object is an instance 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 is a template or blueprint from which objects are created. So, an object is the
instance (result) of a class.
A class in Java can contain: Fields, Methods, Constructors, Blocks, Nested class
and interface
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.
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.
//Java Program to illustrate how to define a class and fields
//Defining a Student class.
class Student{
 //defining fields
 int id;//field or data member or instance variable
 String name;
 //creating main method inside the Student class
 public static void main(String args[]){
  //Creating an object or instance
  Student s1=new Student();//creating an object of Student
  //Printing values of the object
  System.out.println(s1.id);//accessing member through reference variable
  System.out.println(s1.name);
 }
}
Object and Class Example: main outside the class
//Java Program to demonstrate having the main method in
//another class
//Creating Student class.
class Student{
 int id;
 String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
 public static void main(String args[]){
  Student s1=new Student();
  System.out.println(s1.id);
  System.out.println(s1.name);
 }
}

You might also like