Unit 1 Oops Concept
Unit 1 Oops Concept
Unit 1 Oops Concept
Object-oriented programming (OOP) is a programming paradigm based upon objects (having both data and methods) that
aims to incorporate the advantages of modularity and reusability. Objects, which are usually instances of classes, are used
to interact with one another to design applications and computer programs.
The important features of object–oriented programming are −
• Bottom–up approach in program design
• Programs organized around objects, grouped in classes
• Focus on data with methods to operate upon object’s data
• Interaction between objects through functions
• Reusability of design through creation of new classes by adding features to existing classes
Some examples of object-oriented programming languages are C++, Java, Smalltalk, Delphi, C#, Perl, Python, Ruby, and
PHP.
Grady Booch has defined object–oriented programming as “a method of implementation in which programs are organized
as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all
members of a hierarchy of classes united via inheritance relationships”.
The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction,
inheritance, polymorphism, etc.
The main aim of OOP 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.
Some of the striking features of object-oriented
programming are
• Emphasis is on data rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize
the objects.
• Data is hidden and cannot be accessed by external
functions.
• Objects may communicate with each other through
functions.
• New data and functions can be easily added whenever
necessary.
• Follows bottom-up approach in program design.
• 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.
• 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.
• 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.
Objects and Classes in Java
• Object means a real-world entity such as a pen, chair, table, computer, watch, etc.
• 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.
• An object has three characteristics:
o State: represents the data (value) of an object.
o Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
o 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.
• 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.
❑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 can also be defined as a blueprint from which you can create an individual
object. Class doesn't consume any space.
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
Naming a Method
While defining a method, remember that the method name must be a verb and start with a lowercase letter.
If the method name has more than two words, the first name must be a verb followed by adjective or noun.
In the multi-word method name, the first letter of each word must be in uppercase except the first word.
For example:
Single-word method name: sum(), area()
Multi-word method name: areaOfCircle(), stringComparision()
It is also possible that a method has the same name as another method name in the same class, it is known
as method overloading.
Create a Method
• A method must be declared within a class. It is defined with the name of the method, followed by
parentheses ( )
Example Explained
•static means that the method belongs to the Main class and not an
object of the Main class. You will learn more about objects and how
to access methods through objects later.
•void means that this method does not have a return value. You will
learn more about return values later in this chapter
Static vs. Public methods
Method Signature: Every method has a method signature. It is a part of the method declaration. It includes the method
name and parameter list.
Access Specifier: Access specifier or modifier is the access type of the method. It specifies the visibility of the method. Java
provides four types of access specifier:
• Public: The method is accessible by all classes when we use public specifier in our application.
• Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
• Protected: When we use protected access specifier, the method is accessible within the same package or subclasses in a different
package.
• Default: When we do not use any access specifier in the method declaration, Java uses default access specifier by default. It is
visible only from the same package only.
Return Type: Return type is a data type that the method returns. It may have a primitive data type, object, collection, void, etc. If
the method does not return anything, we use void keyword.
Method Name: It is a unique name that is used to define the name of a method. It must be corresponding to the functionality of the
method. Suppose, if we are creating a method for subtraction of two numbers, the method name must be subtraction(). A method is
invoked by its name.
Parameter List: It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It contains the data type
and variable name. If the method has no parameter, left the parentheses blank.
Method Body: It is a part of the method declaration. It contains all the actions to be performed. It is enclosed within the pair of
curly braces.
Types of Method
• There are two types of methods in Java:
Predefined Method
User-defined Method
Predefined Method
• In Java, predefined methods are the method that is already defined in the Java class libraries is known as
predefined methods. It is also known as the standard library method or built-in method. We can directly use
these methods just by calling them in the program at any point. Some pre-defined methods are length(), equals(),
compareTo(), sqrt(), etc. When we call any of the predefined methods in our program, a series of codes related to
the corresponding method runs in the background that is already stored in the library.
• Each and every predefined method is defined inside a class. Such as print() method is defined in
the java.io.PrintStream class. It prints the statement that we write inside the method. For
example, print("Java"), it prints Java on the console.
User-defined Method
• The method written by the user or programmer is known as a user-defined method. These methods are modified
according to the requirement.
//How to Call or Invoke a User-defined Method
public class EvenOdd
{
public static void main (String args[])
{
System.out.print("Enter the number: ");
//method calling
findEvenOdd(num);
}
Constructors in Java
• A constructor in Java is a special method that is used to initialize objects.
• The constructor is called when an object of a class is created. Every time an object is
created using the new() keyword, at least one constructor is called.
• At the time of calling constructor, memory for the object is allocated in the memory.
• It can be used to set initial values for object attributes.
• Note that the constructor name must match the
class name, and it cannot have a return
type (like void).
The following example adds an int y parameter to the constructor. Inside the constructor we set x to
y (x=y). When we call the constructor, we pass a parameter to the constructor (5), which will set the
value of x to 5:
• There are many ways to copy the values of one object into
another in Java. They are:
- By constructor
- By assigning the values of one object into another
- By clone() method of Object class
Access Modifiers in Java
The public keyword is an access modifier, meaning that it is used to set
the access level for classes, attributes, methods and constructors.
We divide modifiers into two groups:
•Access Modifiers - controls the access level
•Non-Access Modifiers - do not control access level, but provides other
functionality
Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the
package. If you do not specify any access level, it will be the default. It provides more accessibility than private.
But, it is more restrictive than protected, and public.
Protected: The access level of a protected modifier is within the package and outside the package through child
class (inheritance). If you do not make the child class, it cannot be accessed from outside the package. The protected
access modifier can be applied on the data member, method and constructor. It can't be applied on the class. It
provides more accessibility than the default modifier.
Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the
class, within the package and outside the package. It has the widest scope among all other modifiers.
Java static keyword
- The static keyword in Java is used for memory management mainly. We can apply static keyword with variables,
methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.
- The static variable can be used to refer to the common property of all objects (which is not unique for each object), for
example, the company name of employees, college name of students, etc. The static variable gets memory only once in the
class area at the time of class loading.
- If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a class.
• A static method can access static data member and can change the value of it.
- Java static block is used to initialize the static data member. It is executed before the main method at the time of
classloading.