Understanding the Basic Concepts of OOP (Object-Oriented Programming)
Table of Content:
Object-oriented programming (or OOP in short) is the dominant programming paradigm these days, having replaced the "structured," procedure-based programming techniques that were developed in the early '70s. Java is a true object oriented programming language. Almost everything in Java is an object.All program code and data reside within objects and classes (also called encapsulation).
You need to understand some of the terminologies of OOP to go further. The most important term is the class and object.
class and object
Class
- The class is at the core of Java. It is the logical construct upon which the entire Java language is built because it defines the shape and nature of an object. As such, the class forms the basis for object-oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class.
- A class is a template or blueprint from which objects are actually made.
- A class is also defined as a new data type, a user defined type which contains two things 1) Data Member 2) Methods
- A class defines the properties and behaviors of objects.
- When you construct an object from a class, you are said to have created an instance of the class.
- The description of a number of the similar object is also called as a class.
- Classes are logical in nature.
Example:
- Furniture is a class, and chair, tables are the example of an object, Furniture does not have any existence but the chair, table do exist. So we can say that Classes is logical in nature.
Syntax for Class
class Circle {
String radius;
void getArea(){
}
void getPerimeter(){
}
}
Explanation of program syntax:
Object
- An object is an instance of a class.
- Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified.
For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects. - Object to object communication is done via methods.
- Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.
An object has a unique identity, state, and behavior.
- The state of an object (also known as its properties or attributes): is represented by data fields with their current values.
A circle object, for example, has a data field radius, which is the property that characterizes a circle. A rectangle object has the data fields width and height, which are the properties that characterize a rectangle.
A dog has states -- color, name, breed - The behavior of an object (also known as its actions): is defined by methods. To invoke a method on an object is to ask the object to perform an action.
For example, you may define methods named getArea() and getPerimeter() for circle objects.
A dog has behaviors -- wagging the tail, barking, eating. - The identity of an object: is defined the object distinguished from others that may have the same behavior and state?
Different ways to create an object in Java
There are many ways to create an object in java. They are:
- By new keyword
- By newInstance() method
- By clone() method
- By deserialization
- By factory method etc
These are simple way to create object
Way 1: Creating Object
Calculation c=new Calculation(); c.cal();
Example
class calculation{
void cal()
{
double a =4, b=23, c=44;
double d;
d = (c+b)/a;
System.out.println(d);
}
}
class MainClass{
public static void main(String args[]){
calculation c=new calculation(); // creation of object
c.cal();
}
}
Output
16.75 Press any key to continue . . .
Way 2: Creating Object
new Calculation();//anonymous object
Example
class calculation{
void cal()
{
double a =4, b=24, c=44;
double d;
d = (c+b)/a;
System.out.println(d);
}
}
class MainClass{
public static void main(String args[]){
new calculation().cal();; // creation of object
}
}
Output
17.0 Press any key to continue . . .
Way 3: Creating Object
new Calculation().cal(6);
Example
class calculation{
void cal(double p)
{
double a, b=32, c=44;
double d;
a=p;
d = (c+b)/a;
System.out.println(d);
}
}
class MainClass{
public static void main(String args[]){
new calculation().cal(6);; // creation of object
}
}
Output
12.666666666666666 Press any key to continue . . .
Creating multiple objects by a single statement
Rectangle r1=new Rectangle(), r2=new Rectangle();//creating two objects
Example
class MainRectangle{
public static void main(String args[]){
Rectangle r1=new Rectangle(),r2=new Rectangle();//creating two objects
r1.insert(10,5);
r2.insert(4,15);
r1.calculateArea();
r2.calculateArea();
}
}
class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;
}
void calculateArea(){
System.out.println("Area = "+length*width);
}
}
Output:
Area = 50 Area = 60 Press any key to continue . . .
- Question 1:
Design a program in Java to calculate the discount given to a customer on purchasing LED Television. The program also displays the amount paid by the customer after discount. The details are given as:
Class name : Television
Data members: int cost, int discount, int amount
Member functions:
Accept( ) : to input the cost of Television
Calculate( ) : to calculate the discount
Display( ) : to show the discount and the amount paid after discount - Question 2: The characteristics of the ___________ objects are considered to be the data members of the ___________ objects.
- Question 3: The ___________ of a class differs on various characteristics.
- Question 4: A real world object deals with characteristics and ___________ .
- Question 5: A class is also considered as an ___________ factory.
- Question 6: Which of the Object Oriented programming principles explain the following illustrations? Justify.
- Question 7: Give two differences between Procedure Oriented languages and Object Oriented languages.
- Question 8: 'Encapsulation reduces the complexity and makes the system easier'. Explain this statement with a real world example.
- Question 9: Give two differences between Data Hiding and Encapsulation.
- Question 10: What is meant by Data Hiding?
- Question 11: Give an example to explain Data Abstraction.
- Question 12: In what way is Data Hiding related to Data Abstraction?
- Question 13: An object of a class is created by using a keyword ___________ .
- Question 14: Class is a ___________ of the objects.
- Question 15: Mention three characteristics and two methods for the following Classes: (a) class Mobile Phone (b) class Bike (c) class Fruits (d) class Pen
- Question 16: Write a statement to create an object 'Keyboard' of the class 'Computer'.
- Question 17: Why is an Object called an 'Instance' of a class? Explain.
- Question 18: Class and Objects are inter-related. Explain.
- Question 19: A statement is given as: 'Study_Table' is an object of the class 'Furniture'. Write down Java statement for the same.
- Question 20: Why is a class known as composite data type?
- Question 21: A class is also referred to as 'Object Factory'. Comment.
- Question 22: What does the following statement mean? Employee staff = new Employee ( );
- Question 23: Define class and object with an example.
- Question 24: How will you define a software object?
- Question 25: ___________ keyword is used for dynamic allocation of an object.
- Question 26: Define the following with an example each. (a) Inheritance (b) Polymorphism
- Question 27: What is meant by Encapsulation?
- Question 28: Write down any two disadvantages of Machine Level Language
- Question 29: Write down two advantages of Polymorphism.
- Question 30: Write the difference between Assembly language and Machine level.
- Question 31: Write the difference between Polymorphism and Encapsulation.
- Question 32: Write the difference between Polymorphism and Encapsulation.
- Question 33: Write any 2 disadvantages of Object-oriented programming.
- Question 34: Define Polymorphism with a real-life example.
- Question 35: Creating ___________ is the fundamental concept in object oriented programming language.
- Question 36: Write the difference between POP and OOP.
- Question 37: Write the difference between High Level Language and Low-Level language?
- Question 38: Write the difference between High Level Language and Low-Level language.
- Question 39: ___________ is the fundamental concept in object oriented programming language.
- Question 40: Objects can communicate with each other through ___________ in object oriented programming language.
- Question 41: Mention two limitations of procedure oriented programming approach.
- Question 42: What is meant by a base class and a derived class?
- Question 43: Why do we prefer object oriented approach in complex programming? Explain.
- Question 44: Name four basic principles of object oriented programming.
- Question 45: What is object oriented programming? Name two object oriented programming languages.
- Question 46: In encapsulation, data can be ___________ or ___________ whereas, in data hiding, data must be ___________ only.
- Question 47: ___________ confirms the security of data members from being manipulated from unauthorized access.
- Question 48: ___________ promotes the reusability feature, in object oriented programming.
- Question 49: Feature of wrapping ___________ and ___________ as a single unit is called encapsulation.
- Question 50: ___________ is an act of representing essential features without including background details.
- Question 51: Using a function for multiple operations is called as ___________ .
- Question 52: What are the supported platforms by Java Programming Language?
Related Questions
- Assignment 1: My First Program In Java
- Assignment 2: Print Your Name Using Java Program