
Java Object Oriented Concepts made easy
- Categories OOPS concepts, OOPS, Java
In this post we will cover Class as the Basis of all Computation in java. You can watch our videos on Object Oriented Concepts – Click Here
Q. 1 What is Object Oriented Programming?
Ans. Object-Oriented Programming or OOPs refers to languages that use objects in programming rather than sequence of functions. They model real world entities or objects.
Object Oriented Languages create software objects which are as close to real world as possible. One of the key purposes of OOPs is to bind together the data and the functions or methods that operate on them in an object so that no other part of the code can access this data except its own functions.
Q. 2 What are 4 key principles of Object Oriented Programming?
Ans. The key principles of Object Oriented Programming are
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
Q. 3 What is Encapsulation?
Ans. Binding (or wrapping) of code and data together into a single unit are known as encapsulation. In object oriented languages a class binds together data and functions and keeps both safe from outside interference and misuse. in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
Java lets classes enforce these access restrictions explicitly by using Access Specifiers of public, private and protected. For example denoting internal data with the private keyword and designating methods intended for use by code outside the class with the public keyword.
Q. 4 What is Abstraction?
Ans. Abstraction is the property by virtue of which you hide the complexity or working inside the object and only the essential details are exposed to the user. The trivial or the non-essentials units are not displayed to the user. In Java, we use abstract class and interface to achieve abstraction.
Q.5 What is Inheritance in Java?
Ans. In real world where we inherit the features of our parents and grandparents and have a family tree or hierarchy. Similarly Object Oriented Languages allow new classes to be formed by inheriting features of parent or base class. When one object acquires the properties and behaviours of a parent object, it is known as inheritance.
It also provides code reusability. For e.g. I can have a class shape from which I can drive sub class Circle, Triangle, Square etc. The data and methods available to the parent class or super class is also available in the child class with the same names. In this case it will inherit all the features of parent or super class. So you have methods like findArea, FindPerimeter which are available to derived class.
Q.6 What is polymorphism?
Ans. Polymorphism means having many forms. It is a mechanism by which behaviour responds differently depending on the object to which it belongs. Polymorphism simplifies usage of object methods by external code and Java takes care of calling the right method with the help of the signature and declaration of these entities
In Java, we use method overloading and method overriding to achieve polymorphism.
- In Method Overloading, we can have multiple methods with same name, like to find area, based upon number of parameters the function can work for different shapes.
- We achieve Method Overriding through Inheritance. In case of inheritance, the derived class can either use the findarea method of base class or it can have its own implementation of findArea method which will then override the base class function. This is called method overriding.
Q. 7 What is a class?
Ans. A class can be defined as a template/blue print that describe the state and behaviours that object of its type support.
Methods – A method is basically a behaviour. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
Instance Variables – Each object has its unique set of instance variables. An object’s state is created by the values assigned to these instance variables.
The class is composed of multiple primitive data types that is why it is known as composite data type.
Example:
class Test{
int x, float y; // Instance Variables or behaviour
boolean result;
public int check() // Method or behaviour
{
System.out.println(result);
}
}
Q. 8 What are objects? Why objects are called instance of the class?
Ans. A Java object is an instance of the class. Java object is combination of data and procedures working on the available data. The object has some characteristics and behaviour. An object is instantiated from a class hence it is also referred to as instance of a class.
Q. 9 What is state and behaviour? Give one example.
Ans. The variables declared within the class represent state and the methods which utilize these variables represent behaviour.
Example:
class Demo{
int x;
char ch; // these are data members represents state
public void show() //working on state or variable represents behaviour
{
x = 39;
x = x 30;
ch += 2;
}
}
Q. 10 Explain local variables, instance variables and class variables?
Ans.
- Local variables: are used inside blocks or in methods as temporary variables and are used to store information needed by that particular block method.
- Instance variables: are used to define attributes or the state of a particular object and are used to store information needed by multiple objects of a class.
- Class variables: are global to a class and to all the instances of the class and are useful for communicating between different objects of all the same classes or keeping track of global states. They are declared using “static” keyword.
Q. 11 What are different accessibility modes in Java?
Ans. The different accessibility modes of java are:
- public
- private
- protected
- default
Q. 12 What is default accessibility mode in Java?
Ans. Default access modifier means we do not explicitly declare an access modifier for a class, field, method etc.
A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
Q. 13 Give the difference between primitive data type and reference data type with one example.
Ans.
Primitive data type | Reference data type |
---|---|
1. The fundamental data types available with Java are known as primitive data type. | 1. They are complex data types constructed by combining primitive data types. |
2. They are defined by Java | 2. They are defined by programmers |
3. They can store only one vale | 3. They can store multiple values either of same type or multiple |
4. Example: int, float, char, double, long, short, byte, boolean. | 4. Example: class, arrays, reference. |
Q. 14 What is a static method or function in Java?
Ans. A static method is a method that belongs to a class rather than an instance of a class.
- The static keyword makes a method/ function as a class function ( also known as static method or function),
- They do not require an instance of class to be created.
- It can access only static methods of the class.
Example:
private static final int c_Year = current_year();
public static int current_year(){
return 2021;
}
Q. 15 What is the difference between a class variable and instance variable?
Ans.
Class variable | Instance variable |
---|---|
1. A data member which is declared only for a class and all the objects of this class shares this data member is known as class variable. | 1. The data member which is created for every object of the class and is not shared is known as instance variable. |
2. A single copy of the variable is accessible to all objects. | 2. Each object holds its own copy of this variable. |
3. The keyword static is used make a variable as class variable. | 3. Only primitive data types are used to make a variable as instance variable. |
4. Example. class data { static int a; // ‘a’ is class variable. } | 4. Example. class data { int p, q; // ‘p’ and ‘q’ are instance //variable. } |
Q. 16 What is static variable?
Ans. The static variable is also known as class variable and declared using keyword “static”. Static variable are shared by all instances or objects of a class.
Example: static int m, n;
You may also like

Questions on Encapsulation

Questions on Library classes
