What is an OOP?
OOPs (Object-Oriented Programming) is a programming paradigm that uses “objects” to represent and manipulate data and behaviour. It is based on the concept of “classes” and “objects,” where a class defines a blueprint for creating objects, and objects are instances of classes. OOPs aims to organize code in a way that models real-world entities and promotes code reusability, maintainability, and modularity.
What are the all concepts of OOP?
Here is a list of all the concepts of Object-Oriented Programming (OOP):
- Class
- Object
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
- Message Passing
- Method Overloading
- Method Overriding
- Constructor
- Destructor
- Access Control
- Data hiding
- Dynamic Binding
- Static Binding
- Composition
- Aggregation
- Association
- Abstract Class
- Interface
- Multiple Inheritance
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Access Specifiers (Public, Private, Protected)
- Abstract Methods
- Method Signature
- Method Resolution Order (MRO)
- Class Variables (Static Variables)
- Instance Variables (Non-Static Variables)
- Method Chaining
- Dependency
- Association, Aggregation, and Composition Relationship
- Class Diagrams
- Object Diagrams
- UML (Unified Modeling Language)
Can you define Class?
A class is a blueprint or a template that defines the properties (attributes) and behaviours (methods) of objects. It acts as a blueprint from which individual objects can be created. In other words, a class is a user-defined data type that encapsulates data and functions related to a particular entity.
Can you define object?
Object concept is termed as a specific instance of a class where the object can be combined of data structures, functions and variables. Its own state, behavior and identity.
Can you define Abstraction?
It is the concept of hiding the internal details and describing things in simple terms.
Write an example of abstraction class and method in java?
In class:
Absrtract class A {}
In method:
Abstract void printstatus ();//no method body and abstract//
Can you define Encapsulation?
Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on that data within a single unit (i.e., the class). It restricts direct access to the data from outside the class and instead provides methods to access and modify the data. This way, the internal implementation details of the class are hidden from the external world, promoting data security and code maintainability.
Write an example of Encapsulation in Java?
In java,Variable name “Job” is kept private or encapsulated.
// save as job.java
Public class job {
Private string name;
Public string getName()
{
return name;
}
Public void setName (string name) {
this.name=name
}
}
// save as test.java
Class test {
Public static void main (string[] args []) {
Job s= new job ();
s.setNname(“InterviewGIG”)
system .out.Println (s.getName ());
}
}
Output: InterviewGIG
Can you define Polymorphism?
Simply, polymorphism means one name many different forms. It is nothing but assigning behaviour or value in a subclass to something that was already declared in the main class. There are two types in polymorphism
Static polymorphism: It is achieved using method Overloading
Dynamic polymorphism: It is using method Overriding
Write an example of polymorphism in Java?
In java, we have two classes: person and employee. The employee class inherits from the person class by using the keyword extends. Here child class overrides the parent class.
Class person {
Void walk () {
System.out.println (“can run…”);
}
}
Class employee extends person {
Void walk () {
system.out.println (“running fast…”);
}
}
Public static void main (string arg [ ]) {
Person p =new employee ();
p.walk ();
}
}
Can you define Inheritance?
Inheritance is a fundamental concept in OOPs that allows a class (called the “child” class or “derived” class) to inherit properties and behaviors from another class (called the “parent” class or “base” class). The child class can reuse the attributes and methods of the parent class without having to define them again. This promotes code reuse and hierarchy in the object-oriented design.
What is the syntax of Inheritance in Java?
In java, inheritance can be used the extends keyword
Class interview {
}
Class GIG extends interview {
}
What is the main difference between an Object based programming language and Object-oriented programming language?
Object oriented programming is to implement all concepts like Object, abstraction, inheritance, classes, polymorphism etc. object based programming language follows all the concepts of OOPs except inheritance such as JavaScript, VBscript.
Can you define association?
It is relationship between two objects with multiplicity.
Can you define composition?
Composition is about expressing relationships between objects. A chair example: A chair has a Seat. A chair has a back. And a chair has a set of legs. The phrase “has a” implies a relationship where the chair owns, or at minimum, uses, another object. It is this “has a” relationship which is the basis for composition.
Can you aggregation?
Aggregation is also known as “HAS-A” relationship. Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is an ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.
What is object clone?
The object is away to create exact copy of an object. The clone() method of object class is used to clone an object.
Can you define constructor?
It is a method used to initialize the state of an object, and it gets invoked at the time of object creation. Rules for constructor are: Constructor Name should be same as class name and Constructor must have no return type
Can you define Destructor?
It is a method which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.
Can you define Inline function?
Inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.
Can you define virtual function?
Virtual function is a member function of class and its functionality can be overridden in its derived class. This function can be implemented by using a keyword called virtual, and it can be given during function declaration.
Virtual function can be achieved in C++, and it can be achieved in C Language by using function pointers or pointers to function.
Can you define abstract class?
An abstract class is a class which cannot be instantiated. Creation of an object is not possible with abstract class, but it can be inherited. An abstract class can contain only Abstract method.
Can you define interface?
An interface is a collection of abstract method. If the class implements an inheritance, and then thereby inherits all the abstract methods of an interface.
Can you explain friend function?
Friend function is a friend of a class that is allowed to access to Public, private or protected data in that same class. If the function is defined outside the class cannot access such information.
Friend can be declared anywhere in the class declaration, and it cannot be affected by access control keywords like private, public or protected.
Can you explain ternary operator?
Ternary operator is said to be an operator which takes three arguments. Arguments and results are of different data types, and it is depending on the function. Ternary operator is also called as conditional operator.
Can you explain super keyword?
Super keyword is used to invoke overridden method which overrides one of its super class methods. This keyword allows to access overridden methods and also to access hidden members of the super class.
It also forwards a call from a constructor to a constructor in the super class.
Can you explain exception handling?
Exception is an event that occurs during the execution of a program. Exceptions can be of any type – Run time exception, Error exceptions. Those exceptions are handled properly through exception handling mechanism like try, catch and throw keywords.
Can you explain tokens?
Token is recognized by a compiler and it cannot be broken down into component elements. Keywords, identifiers, constants, string literals and operators are examples of tokens.
Even punctuation characters are also considered as tokens – Brackets, Commas, Braces and Parentheses.
What is the difference between structure and a class?
Structure default access type is public, but class access type is private. A structure is used for grouping data whereas class can be used for grouping data and methods. Structures are exclusively used for data and it doesn’t require strict validation, but classes are used to encapsulates and inherit data which requires strict validation
How can we call the base method without creating an instance?
Yes, it is possible to call the base method without creating an instance. And that method should be Static method.
Doing inheritance from that class. -Use Base Keyword from derived class.
Why java is does not support multiple inheritances?
Java is a simple programming language and multiple introduce complexities such as diamond problem. Inheriting stages from two different type of classes is a case which is in reality very rare and it can be achieved easily through an object association.
What are the pros of Oops programming?
- It is simplicity
- It is modularity
- It is modifiability
- It is extensibility
- It is maintainability
- It is Reusability
- Effective problem solving
What are the types of inheritance?
There are five types of Inheritance:
- Single Inheritance
- Multiple Inheritance
- Multi-level Inheritance
- Hybrid Inheritance
- Hierarchical Inheritance
What are the types of constructor?
There are five types of constructors:
- Default Constructor
- Private Constructor
- Copy Constructor
- Static Constructor
- Parameterized Constructor
What is syntax of constructor declaration in python?
def __init__(self):
# body of the constructor
Give an example of constructor in python?
When we declare a constructor
In this case, python does not create a constructor in our program.
Example:
class DemoClass:
num = 101
# non-parameterized constructor
def __init__(self):
self.num = 420
# a method
def read_number(self):
print(self.num)
# creating object of the class
obj = DemoClass()
# calling the instance method using the object obj
obj.read_number()
Output:
420
How to Crate empty Class in Python?
To create empty class using pass statement:
Class test;
Pass
When the finalize method is used?
In OPPs, finalize method is called just before the object is about to be garbage collected. This method overrides to minimize memory leaks, undertake cleanup activities by removing system resources.
How to create class & object in python?
Example:
class Student:
# instance attributes
def __init__(self, name, age): // self is an instant of Class
self.name = name
self.age = age
# creating objects of class student
Mohan = student("mohan",25)
Ram = student("Ram",26)
# accessing object information
print("age of {} is {}".format(mohan.name, mohan.age))
print("age of {} is {}".format(ram.name, ram.age))
{or print( mohan. __init__) //prints dictionary}
Output:
age of Mohan is 25
age of Ram is 26
Give an Example of encapsulation in Python?
Example of Encapusulation:
class student(object):
def __init__(self):
self.name = Mohan
self._age = 25
object1 = student()
print(object1.name)
print(object1._age)
output:
mohan
25
How to achieve data abstraction?
Data abstraction can be achieved through:
- Abstract method
- Abstract class
What are some examples of tokens?
Here are some common examples of tokens:
- Keywords
- Operators
- Strings
- Constants
- Identifiers
- Commas
- Brackets
Give an example of abstraction in Python?
from abc import ABC,abstractmethod
class Student (ABC):
def stu_id(self,name,age): //Abstraction
pass
class childstudent1(student):
def stu_id(self,age):
print("stu_age is 25")
stu1 = childstu1()
stu1.stu_age(age)
Output:
stu_age is 25
Give an example of overloading less than operator in Python?
# overloading the less than operator
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x, self.y)
def __lt__(self, other):
self_mag = (self.x ** 2) + (self.y ** 2)
other_mag = (other.x ** 2) + (other.y ** 2)
return self_mag < other_mag
p1 = Point(2,3)
p2 = Point(-4,-5)
p3 = Point(1,-1)
# use less than
print(p1<p2)
print(p2<p3)
print(p1<p3)
output:
true
false
false
What are the languages come under oops concept?
Simula is known as the first OOP Language.
- C# Language45r
- C++ Language
- Java Language
- Python Language
- JavaScript Language
- PHP Language
- Ruby Language
- Scala Language
- Visualbasic.NET Language