SNS COLLEGE OF ENGINEERING
UNIT I
Object-Oriented Programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of
“objects”, which may contain data, in the form of fields, often known as attributes; and code, in the
form of procedures, often known as methods.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a
methodology or paradigm to design a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:
● Object
● Classes
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation
Objects And Classes
Object Objects have states and behaviours. Example: A dog has states - colour, name, breed
as well as behaviours – wagging the tail, barking, eating. An object is an instance of a class.
Class
A class can be defined as a template/blueprint that describes the behaviour/state that the
object of its type support.
Objects 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.
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 1
, SNS COLLEGE OF ENGINEERING
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:
● An object is a real-world entity.
● An object is a runtime entity.
● The object is an entity which has state and behavior.
● The object is an instance of a class.
Classes 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
Following is an example of a class.
public class Dog
{
String breed;
int age;
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 2
, SNS COLLEGE OF ENGINEERING
String color;
void barking()
{}
}
A class can contain any of the following variable types.
• Local variables − Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable will be
destroyed when the method has completed.
• Instance variables − Instance variables are variables within a class but outside any method. These
variables are initialized when the class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
• Class variables − Class variables are variables declared within a class, outside any method, with the
static keyword. A class can have any number of methods to access the value of various kinds of
methods. In the above example, barking (), hungry () and sleeping () are methods.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse.
● In Java, the basis of encapsulation is the class. There are mechanisms for hiding the
complexity of the implementation inside the class.
● Each method or variable in a class may be marked private or public.
● The public interface of a class represents everything that external users of the class need
to know, or may know.
● The private methods and data can only be accessed by code that is a member of the class.
● Therefore, any other code that is not a member of the class cannot access a private
method or variable.
● Since the private members of a class may only be accessed by other parts of program
through the class’ public methods, we can ensure that no improper actions take place.
Inheritance
Inheritance is the process by which one object acquires the properties of another object.
Inheritance is an important pillar of OOP (Object-Oriented Programming). It is the mechanism in Java
by which one class is allowed to inherit the features (fields and methods) of another class.
In Java, Inheritance means creating new classes based on existing ones. A class that inherits
from another class can reuse the methods and fields of that class.
Java Inheritance Types
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface to
be used for a general class of actions. The specific action is determined by the exact nature of the
situation.
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 3
, SNS COLLEGE OF ENGINEERING
For eg., a dog’s sense of smell is polymorphic. If the dog smells a cat, it will bark and run
after it. If the dog smells its food, it will salivate and run to its bowl. The same sense of smell is at
work in both situations. The difference is what is being smelled, that is, the type of data being
operated upon by the dog’s nose.
Consider a stack (which is a last-in, first-out LIFO list). We might have a program that
requires three types of stacks. One stack is used for integer values, one for floating-point values, and
one for characters. The algorithm that implements each stack is the same, even though the data being
stored differs.
Platform independent: Unlike many other programming languages including C and C++, when Java
is compiled, it is not compiled into platform specific machine, rather into platform independent byte
code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on
whichever platform it is being run.
Features of JAVA
The primary objective of Java programming language creation was to make it portable, simple
and secure programming language. Apart from this, there are also some excellent features which play
an important role in the popularity of this language. The features of Java are also known as Java
buzzwords.
A list of the most important features of the Java language is given below.
● Simple
● Object-Oriented
● Portable
● Platform independent
● Secured
● Robust
● Architecture neutral
● Interpreted
● High Performance
● Multithreaded
● Distributed
● Dynamic
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to
Sun Microsystem, Java language is a simple programming language because:
Java syntax is based on C++ (so easier for programmers to learn it after C++).
Object-Oriented
Java is an object-oriented programming language. Everything in Java is an object.
Object-oriented means we organize our software as a combination of different types of objects that
incorporate both data and behaviour.
Object-oriented programming (OOPs) is a methodology that simplifies software development
and maintenance by providing some rules.
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 4
UNIT I
Object-Oriented Programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of
“objects”, which may contain data, in the form of fields, often known as attributes; and code, in the
form of procedures, often known as methods.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a
methodology or paradigm to design a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:
● Object
● Classes
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation
Objects And Classes
Object Objects have states and behaviours. Example: A dog has states - colour, name, breed
as well as behaviours – wagging the tail, barking, eating. An object is an instance of a class.
Class
A class can be defined as a template/blueprint that describes the behaviour/state that the
object of its type support.
Objects 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.
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 1
, SNS COLLEGE OF ENGINEERING
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:
● An object is a real-world entity.
● An object is a runtime entity.
● The object is an entity which has state and behavior.
● The object is an instance of a class.
Classes 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
Following is an example of a class.
public class Dog
{
String breed;
int age;
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 2
, SNS COLLEGE OF ENGINEERING
String color;
void barking()
{}
}
A class can contain any of the following variable types.
• Local variables − Variables defined inside methods, constructors or blocks are called local
variables. The variable will be declared and initialized within the method and the variable will be
destroyed when the method has completed.
• Instance variables − Instance variables are variables within a class but outside any method. These
variables are initialized when the class is instantiated. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
• Class variables − Class variables are variables declared within a class, outside any method, with the
static keyword. A class can have any number of methods to access the value of various kinds of
methods. In the above example, barking (), hungry () and sleeping () are methods.
Encapsulation
Encapsulation is the mechanism that binds together code and the data it manipulates, and
keeps both safe from outside interference and misuse.
● In Java, the basis of encapsulation is the class. There are mechanisms for hiding the
complexity of the implementation inside the class.
● Each method or variable in a class may be marked private or public.
● The public interface of a class represents everything that external users of the class need
to know, or may know.
● The private methods and data can only be accessed by code that is a member of the class.
● Therefore, any other code that is not a member of the class cannot access a private
method or variable.
● Since the private members of a class may only be accessed by other parts of program
through the class’ public methods, we can ensure that no improper actions take place.
Inheritance
Inheritance is the process by which one object acquires the properties of another object.
Inheritance is an important pillar of OOP (Object-Oriented Programming). It is the mechanism in Java
by which one class is allowed to inherit the features (fields and methods) of another class.
In Java, Inheritance means creating new classes based on existing ones. A class that inherits
from another class can reuse the methods and fields of that class.
Java Inheritance Types
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
Polymorphism
Polymorphism (from Greek, meaning “many forms”) is a feature that allows one interface to
be used for a general class of actions. The specific action is determined by the exact nature of the
situation.
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 3
, SNS COLLEGE OF ENGINEERING
For eg., a dog’s sense of smell is polymorphic. If the dog smells a cat, it will bark and run
after it. If the dog smells its food, it will salivate and run to its bowl. The same sense of smell is at
work in both situations. The difference is what is being smelled, that is, the type of data being
operated upon by the dog’s nose.
Consider a stack (which is a last-in, first-out LIFO list). We might have a program that
requires three types of stacks. One stack is used for integer values, one for floating-point values, and
one for characters. The algorithm that implements each stack is the same, even though the data being
stored differs.
Platform independent: Unlike many other programming languages including C and C++, when Java
is compiled, it is not compiled into platform specific machine, rather into platform independent byte
code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on
whichever platform it is being run.
Features of JAVA
The primary objective of Java programming language creation was to make it portable, simple
and secure programming language. Apart from this, there are also some excellent features which play
an important role in the popularity of this language. The features of Java are also known as Java
buzzwords.
A list of the most important features of the Java language is given below.
● Simple
● Object-Oriented
● Portable
● Platform independent
● Secured
● Robust
● Architecture neutral
● Interpreted
● High Performance
● Multithreaded
● Distributed
● Dynamic
Simple
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to
Sun Microsystem, Java language is a simple programming language because:
Java syntax is based on C++ (so easier for programmers to learn it after C++).
Object-Oriented
Java is an object-oriented programming language. Everything in Java is an object.
Object-oriented means we organize our software as a combination of different types of objects that
incorporate both data and behaviour.
Object-oriented programming (OOPs) is a methodology that simplifies software development
and maintenance by providing some rules.
19CS206-Object Oriented Programming/Ms.M.DEEPIKA,AP/CSE Page 4