CHPT 1 - What Is OOPs Concept
CHPT 1 - What Is OOPs Concept
CHPT 1 - What Is OOPs Concept
These objects always correspond to things found in the real world, i.e.,
real entities. So, they are also called run-time entities of the world. These
are self–contained which consists of methods and properties which make
data useful. Objects can be both physical and logical data. It contains
addresses and takes up some space in memory. Some examples of
objects are a dog, chair, tree etc.
When we treat animals as objects, it has states like colour, name, breed
etc., and behaviours such as eating, wagging the tail etc.
System.out.println(Myobj.x);
We can also create multiple objects in the same class and we can create
in one class and access it in another class. This method is used for better
organization of classes and always remember that name of the java file
and the class name remains the same.
Example 2:
The below example shows how multiple objects are created in the same
class and how they are accessed from another class.
● Mybook.java
Public class Mybook {
int x=10;
1 int y=8;
}
● Count.java
Class Count {
Public static void main (String [] args)
{
Mybook myobj1 = new myobj1();
Mybook myobj2 = new myobj2();
System.out.println (myobj1.x);
System.out.println (myobj2.y);
}
}
class classname {
type instance variable 1;
type instance variable 2;
.
.
.
type instance variable n;
type methodname 1 (parameter list) {
// body od method
}
type methodname 2 (parameter list) {
// body od method
}
type methodnamen (parameter list) {
// body od method
}
}
The variables or data defined within a class are called instance variables.
Code is always contained in the methods. Therefore, the methods and
variables defined within a class are called members of the class. All the
methods have the same form as the main () these methods are not
specified as static or public.
What is Abstraction?
Data Abstraction is the property by virtue of which only the essential
details are displayed to the user. The trivial or non-essential units are not
displayed to the user.
Ex: A car is viewed as a car rather than its individual components.
//abstract class
abstract class GFG{
//abstract methods declaration
abstract void add();
abstract void mul();
abstract void div();
}
What is Inheritance?
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. We
are achieving inheritance by using extends keyword. Inheritance is also
known as “is-a” relationship.
Demonstration of Inheritance :
sleep(1000) //millis
sleep(1000,2000) //millis,nanos
// Overloaded sum().
// This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum().
// This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum().
// This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Output
30
60
31.0
Polymorphism in Java is mainly of 2 types:
1. Overloading
2. Overriding
What is Encapsulation?
It is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together the code and the data it manipulates.
Another way to think about encapsulation is that it is a protective shield
that prevents the data from being accessed by the code outside this
shield.
● Technically, in encapsulation, the variables or the data in a class is
hidden from any other class and can be accessed only through any
member function of the class in which they are declared.
● In encapsulation, the data in a class is hidden from other classes,
which is similar to what data-hiding does. So, the terms
“encapsulation” and “data-hiding” are used interchangeably.
● Encapsulation can be achieved by declaring all the variables in a
class as private and writing public methods in the class to set and get
the values of the variables.
Demonstration of Encapsulation:
● Re-usability
When we say re-usability, it means that “write once, use it multiple
times” i.e., reusing some facilities rather than building it again and
again, which can be achieved by using class. We can use it n number of
times whenever required.
● Data redundancy
It is one of the greatest advantages in oops. This is the condition which
is created at the data storage when the same piece of data is held at two
different places. If we want to use similar functionality in multiple
classes, we can just write common class definitions for similar
functionalities by inheriting them.
● Code maintenance
It is easy to modify or maintain existing code as new objects which can
be created with small differences from the existing ones. This helps
users from doing rework many times and modifying the existing codes
by incorporating new changes to it.
● Security
Data hiding and abstraction are used to filter out limited exposure which
means we are providing only necessary data to view as we maintain
security.
● Design benefits
The designers will have a long and more extensive design phase, which
results in better designs. At a point of time when the program has
reached critical limits, it will be easier to program all non-oops
separately.
● Easy troubleshooting
Using encapsulation objects is self-constrained. So, if developers face
any problem easily it can be solved. And there will be no possibility of
code duplicity.
● Flexibility
● Problem-solving
hese are divided into small parts called objects. It follows a top-down approach.
Adding new functions or data is easy. Adding new data and functions is not easy.
Examples are c++, java, python etc. Examples FORTRAN, Cobol etc.
Basic programming constructs:EXP 1
❖ This program will try to print “Hello World” 5 times.
class whileLoopDemo {
public static void main(String args[])
{
// initialization expression
int i = 1;
// test expression
while (i < 6) {
System.out.println("Hello World");
// update expression
i++;
}
}
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
// Do-while loop
do {
// Update expression
i++;
}
// Test expression
while (i < 6);
}
}
Output:
Hello World
Hello World
Hello World
Hello World
Hello World