0% found this document useful (0 votes)
13 views8 pages

OOPS Training Concepts

Uploaded by

mohammed arfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

OOPS Training Concepts

Uploaded by

mohammed arfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Object Oriented Programming (OOPs)

Java - What is OOP?


Object-Oriented Programming or Java OOPs concept refers to
languages that use objects in programming, they use objects as a
primary source to implement what is to happen in the code. Objects are
seen by the viewer or user, performing tasks you assign.

 Object-oriented programming has several advantages over


procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the Java code DRY "Don't Repeat Yourself",
and makes the code easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with
less code and shorter development time

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.
But, it is not forced to follow. So, it is known as convention not rule.
These conventions are suggested by several Java communities such as
Sun Microsystems and Netscape.

All the classes, interfaces, packages, methods and fields of Java


programming language are given according to the Java naming
convention. If you fail to follow these conventions, it may generate
confusion or erroneous code.

Advantage of Naming Conventions in Java


 By using standard Java naming conventions, you make your code
easier to read for yourself and other programmers.
 Readability of Java program is very important.
 It indicates that less time is spent to figure out what the code does.

Naming Conventions of the Different Identifiers


The following table shows the popular conventions used for the different
identifiers.

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, ActionListener. //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 class Employee


as id, name. {
It should not start with the special // variable
characters like & (ampersand), $ (dollar), _ int id;
(underscore). //code snippet
If the name contains multiple words, start it }
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.javatpoint;
If the name contains multiple words, it class Employee
should be separated by dots (.) such as {
java.util, java.lang. //code snippet
}

Constant It should be in uppercase letters such as class Employee


RED, YELLOW. {
If the name contains multiple words, it //constant
should be separated by an underscore(_) static final int MIN_AGE =
such as MAX_PRIORITY. 18;
It may contain digits but not as the first //code snippet
letter. }

Java - What are Classes and Objects?


Classes and objects are the two main aspects of object-oriented
programming.

Look at the following illustration to see the difference between class and
objects:
EX: 1

Class Objects

 Fruit Apple
Banana
Mango
EX: 2

Class Objects

 Car Volvo
Audi
Toyota

 So, a class is a template for objects, and an object is an instance of a


class.
 When the individual objects are created, they inherit all the variables
and methods from the class.

OOPS concepts are as follows :


1. Class
2. Object
3. Pillars of OOPs
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
 CLASS

A class is a user-defined blueprint or prototype from which objects are


created. It represents the set of properties or methods that are common
to all objects of one type. Using classes, you can create multiple
objects with the same behaviour instead of writing their code multiple
times. This includes classes for objects occurring more than once in
your code.

1. State: It is represented by the attributes of an object. It also reflects


the properties of an object.
2. Behaviour: It is represented by the methods of an object. It also
reflects the response of an object to other objects.
3. Identity: It is a unique name given to an object that enables it to
interact with other objects.
4. Method: A method is a collection of statements that perform some
specific task and return the result to the caller. A method can
perform some specific task without returning anything. Methods allow
us to reuse the code without retyping it, which is why they are
considered time savers. In Java, every method must be part of some
class, which is different from languages like C, C++, and Python.

OBJECT
 An object is a basic unit of Object-Oriented Programming that
represents real-life entities. A typical Java program creates many
objects, which as you know, interact by invoking methods. The objects
are what perform your code, they are the part of your code visible to
the viewer/user.

An object has three characteristics:


1. State: represents the data (value) of an object.
2. Behaviour: represents the behaviour (functionality) of an object
such as deposit, withdraw, etc.
3. 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; colour is white,


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

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.

Object Definitions:

o An object is a real-world entity.


o An object is a runtime entity.
o The object is an entity which has state and behaviour.
o The object is an instance of a class.

class and objects one simple java program


public class GFG {

static String Employee name;


static float Employee salary;

static void set (String n, float p) {


Employee name = n;
Employee salary = p;
}

static void get () {


System.out.println("Employee name is: " +Employee name );
System.out.println("Employee CTC is: " + Employee salary);
}

public static void main(String args[]) {


GFG.set("Rathod Avinash", 10000.0f);
GFG.get();
}
}

Output

Employee name is : Rathod avinash


Employee CTC is: 10000.0

Inheritance
When one object acquires all the properties and behaviours of a parent
object, it is known as inheritance. It provides code reusability. It is used
to achieve runtime polymorphism.

Abstraction
Hiding internal details and showing functionality is known as abstraction.

 For example, phone call, we don't know the internal processing.


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.

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.

You might also like