• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

OOPS concepts

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

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. 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.

  1. Local variables: are used inside blocks or in methods as temporary variables and are used to store information needed by that particular block method.
  2. 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.
  3. 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:

  1. public
  2. private
  3. protected
  4. 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 typeReference 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 Java2. They are defined by programmers
3. They can store only one vale3. 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 variableInstance 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;

  • Share:
author avatar
Simply Coding

Previous post

Short Questions on Package/Wrapper classes
June 22, 2021

Next post

Arrays in Java
June 22, 2021

You may also like

Questions on Encapsulation
Questions on Encapsulation
4 July, 2021
Questions on Library classes
Questions on Library classes
4 July, 2021
Questions on String 1
What is Java String?
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now