MOST IMPORTANT QUES
MOST MOST REPEATED QUES
OOP Concept
Object Oriented Programming is a methodology or paradigm to design a program using classes
and object. Its simplifies the software development and maintenance by providing some
concepts like object ,class ,inheritance ,polymorphism, abstraction and encapsulation.
Object is a instance/variable of class. Any entity that has state and behaviour is known as
object. For example: chair, pen, table. It can be physical and logical.
Class is a blueprint or template for creating objects. It defines the properties (attributes) and
behaviors (methods) that the objects instantiated from the class will have.It does not take any
space in memory.
Condition for a language to be a 100% OOPs
All datatypes must be objects. There should be no primitive type that exists independently of
objects.
Follows Inheritance, Polymorphism, Encapsulation, and Abstraction.
No static method is allowed.
No global function/variable is allowed.
C:
C is not considered an OOPs language.
C is a procedural language, i.e., it focuses on procedures (functions) and structural
programming.
It does not support the OOPs concept.
C++:
C++ is a partially OOPs language.
C++ is a multi-paradigm language. It supports both procedural programming (like C)
and OOP.
C++ adds OOP features like Inheritance, Polymorphism, Encapsulation, and
Abstraction to C.
C++ has primitive data types (int, char, float, etc.) which are not objects.
Global functions & variables are allowed in C++.
JAVA:
JAVA is not a 100% OOP language.
JAVA has 8 primitive datatypes (int, byte, short, char, long, float, double, boolean). These
are not objects.
JAVA can use static methods and variables.
JAVA allows global functions/variables.
PYTHON:
PYTHON is largely but not 100% OOP language.
In Python, everything is an object, but we can still use primitive datatypes like int and float.
Thus, Python hides the object nature of these primitives to simplify code.
Python is multi-paradigm (like C++).
Global functions are allowed.
Static methods are also allowed in Python.
Smalltalk is considered a 100% pure OOP language
Note: Python > Java > C++ > C
WWW.PRIMECODING.IN
MASSIVE SUCCESS RATE
"Transform Your Interview Opportunity into an Offer Letter and Make Your Parents
Proud!"
In-depth Technical Mock
Crack coding challenges with real experts.
HR & Managerial Prep
Master behavioral questions and impress Cognizant
Interviewer.
Full Interview Simulation
Ace both technical and HR in one session.
Resume Review
Identify and fix weaknesses for a standout CV.
Personalized Feedback & Expert Guidance
Tailored improvement tips to boost success.
www.primecoding.in
INHERITANCE
Inheritance is a mechanism in which a subclass extends all the properties of the superclass.
extends keyword is used.
It provides code reusability.
We can't access the private members of a class through inheritance.
Method overriding is only possible through inheritance.
class Employee {
float salary = 40000;
}
class Programmer extends Employee {
int bonus = 10000;
public static void main(String args[]) {
Programmer p = new Programmer();
System.out.println("Programmer salary is: " + p.salary);
System.out.println("Bonus of Programmer is: " + p.bonus);
}
}
class Employee:
def __init__(self):
self.salary = 40000
class Programmer(Employee):
def __init__(self):
super().__init__() # Call parent class constructor
self.bonus = 10000
# Creating an object of Programmer class
p = Programmer()
print("Programmer salary is:", p.salary)
print("Bonus of Programmer is:", p.bonus)
Output:
Programmer salary is: 40000.0
Bonus of Programmer is: 10000
Java does not support multiple inheritance through classes
because whenever a sub-class wants to inherit the property of
two or more superclasses that have the same method, the Java
compiler can't decide which class method it should inherit.
Then there might be a chance of memory duplication. Java
supports multiple inheritance using interface.
Python support multiple inheritance. It uses the Method
Resolution Order (MRO) to decide which method to execute
when multiple parent classes have the same method.
class GrandParent {
void greet() {
System.out.println("Hello from Grandparent!");
}
}
class Parent extends GrandParent {
void display() {
System.out.println("This is the Parent class.");
}
}
class Child extends Parent {
void show() {
System.out.println("This is the Child class.");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.greet(); //Hello from Grandparent
obj.display(); //This is the Parent class
obj.show(); //This is the Child class
}
}
Multilevel Heritance
class GrandParent:
def greet(self):
print("Hello from Grandparent!")
class Parent(GrandParent):
def display(self):
print("This is the Parent class.")
class Child(Parent):
def show(self):
print("This is the Child class.")
obj = Child()
obj.greet()
obj.display()
obj.show()
class Parent {
void display() {
System.out.println("This is the Parent class.");
}
}
class Child1 extends Parent {
void show1() {
System.out.println("This is Child1 class.");
}
}
class Child2 extends Parent {
void show2() {
System.out.println("This is Child2 class.");
}
}
public class Main {
public static void main(String[] args) {
Child1 obj1 = new Child1();
Child2 obj2 = new Child2();
obj1.display(); //This is the Parent class
obj1.show1(); //This is Child1 class
obj2.display(); //This is the Parent class
obj2.show2(); //This is Child2 class
}
}
Hierarchical Heritance
class Parent:
def display(self):
print("This is the Parent class.")
class Child1(Parent):
def show1(self):
print("This is Child1 class.")
class Child2(Parent):
def show2(self):
print("This is Child2 class.")
# Creating objects of Child1 and Child2
obj1 = Child1()
obj2 = Child2()
obj1.display()
obj1.show1()
obj2.display()
obj2.show2()
Polymorphism is when one task is performed by different ways.
1. Compile-time polymorphism:
A polymorphism which exists at the time of compilation.
Example: Method Overloading
Whenever a class contains more than one method with the same name but having different numbers or
different types of parameters, it is called method overloading.
Polymorphism
class A {
void add() {
int a = 10, b = 20, c;
c = a + b;
System.out.println(c);
}
void add(int a, int b) {
int c = a + b;
System.out.println(c);
}
void add(int a, double b) {
double c = a + b;
System.out.println(c);
}
public static void main(String[] args) {
A obj = new A();
obj.add(); // Output: 30
obj.add(20, 30); // Output: 50
obj.add(10, 20.5); // Output: 30.5
} }
class A:
def add(self, a=10, b=20):
print(a + b)
obj = A()
obj.add() # Output: 30
obj.add(20, 30) # Output: 50
obj.add(10, 20.5) # Output: 30.5
Python does not support method
overloading in the way Java does
(where multiple methods with the
same name but different parameters
exist). Instead, we can simulate
method overloading using default
arguments.
2.Compile-time polymorphism:
A polymorphism which exists at the time of execution of the program.
Example: Method Overriding
Whenever the subclass & superclass have the same method (name, return type, parameter), it is called
method overriding.
class shape {
void draw() {
System.out.println("I can't say shape type");
}
}
class square extends shape {
void draw() {
System.out.println("square shape");
}
}
class Test {
public static void main(String[] args) {
square s = new square();
s.draw(); // square shape
}
}
class Shape:
def draw(self):
print("I can't say shape type")
class Square(Shape):
def draw(self):
print("square shape")
# Creating an object of Square class
s = Square()
s.draw() // square shape
Encapsulation
Encapsulation is the mechanism of binding the data & the code acting on the data into a single unit.
Example: A Java class
Steps to achieve Encapsulation:
Declare the variable of the class as private.
Declare public setter & getter methods to modify & view the variable's value.
Data Hiding:
In encapsulation, the variables of a class will be hidden from other classes & can be accessed only through
the methods of their current class. This is the concept of data hiding.
class Employee {
private int salary; // This is data hiding
public void setter(int x) {
salary = x;
}
public int getter() {
return salary;
}
}
class Test {
public static void main(String[] args) {
Employee e = new Employee();
e.setter(100000);
System.out.println(e.getter()); //100000
}
}
class Employee:
def __init__(self):
self.__salary = 0 # Private variable (data hiding)
def set_salary(self, x):
self.__salary = x
# Getter method
def get_salary(self):
return self.__salary
# Test class
if __name__ == "__main__":
e = Employee()
e.set_salary(100000)
print(e.get_salary()) #100000
Abstraction
Abstraction is hiding the implementation details & showing the
functionalities. Its advantages are security & enhancement.
Here enhancement means, for example, a calculator is coded in C++
which makes it slower. Now, we have changed it to Python. But the user
sees the same button unaware of this change.
Abstraction can be achieved by:
Abstract class
Interface
In Java, abstract class is a class that is declared with abstract keyword.
We can not create object for abstract class, but we can create its reference variable.
It may or may not contain abstract methods
It can contain abstract and non-abstract methods
To use the abstract class, we have to create its subclass
If a class contains partial implementation(no body), then we should declare a class as
abstract
Abstract Method:
A method that contains the abstract keyword at the time of declaration is called an abstract method
It can only be used in abstract classes & interface
It does not contain any body or implementation
An abstract method must be overridden in a subclass, otherwise, the subclass will also become abstract class
Whenever an action is common but implementation is different, we should use an abstract class
abstract class Animal {
public abstract void sound(); // Abstract method
}
class Dog extends Animal {
public void sound() {
System.out.println("Dogs bark");
}
}
class Cat extends Animal {
public void sound() {
System.out.println("Cats meow");
}
}
class Test {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // Calls Dog's sound() method
Cat c = new Cat();
c.sound(); // Calls Cat's sound() method
}
}
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass # Abstract method
class Dog(Animal):
def sound(self):
print("Dogs bark")
class Cat(Animal):
def sound(self):
print("Cats meow")
# Testing the subclasses
d = Dog()
d.sound() # Calls Dog's sound() method
c = Cat()
c.sound() # Calls Cat's sound() method
In Java , Interface is just like a class that contains only abstract methods .To achieve an interface, we
use the implements keyword. An interface is a contract that defines a set of methods without
providing any implementation.
Interface methods are by default public and abstract.
Interface variables are by default public, static, and final.
Interface methods must be overridden inside the implementing classes.
An interface is nothing but a bridge between a client and a developer.
Interfaces help achieve abstraction and multiple inheritance in Java.
interface Drawable {
void draw();
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("Drawing rectangle");
}
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing circle");
}
}
class TestInterface {
public static void main(String args[]) {
Drawable d = new Circle();
d.draw(); // drawing circle
}
}
Claim this material
on topmate!
COGNIZANT REPEATED COGNIZANT REPEATEDCOGNIZANT REPEATED
COGNIZANT REPEATED COGNIZANT REPEATEDCOGNIZANT REPEATED

COGNIZANT REPEATED COGNIZANT REPEATEDCOGNIZANT REPEATED

  • 1.
    MOST IMPORTANT QUES MOSTMOST REPEATED QUES
  • 4.
    OOP Concept Object OrientedProgramming is a methodology or paradigm to design a program using classes and object. Its simplifies the software development and maintenance by providing some concepts like object ,class ,inheritance ,polymorphism, abstraction and encapsulation. Object is a instance/variable of class. Any entity that has state and behaviour is known as object. For example: chair, pen, table. It can be physical and logical. Class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects instantiated from the class will have.It does not take any space in memory.
  • 5.
    Condition for alanguage to be a 100% OOPs All datatypes must be objects. There should be no primitive type that exists independently of objects. Follows Inheritance, Polymorphism, Encapsulation, and Abstraction. No static method is allowed. No global function/variable is allowed. C: C is not considered an OOPs language. C is a procedural language, i.e., it focuses on procedures (functions) and structural programming. It does not support the OOPs concept. C++: C++ is a partially OOPs language. C++ is a multi-paradigm language. It supports both procedural programming (like C) and OOP. C++ adds OOP features like Inheritance, Polymorphism, Encapsulation, and Abstraction to C. C++ has primitive data types (int, char, float, etc.) which are not objects. Global functions & variables are allowed in C++.
  • 6.
    JAVA: JAVA is nota 100% OOP language. JAVA has 8 primitive datatypes (int, byte, short, char, long, float, double, boolean). These are not objects. JAVA can use static methods and variables. JAVA allows global functions/variables. PYTHON: PYTHON is largely but not 100% OOP language. In Python, everything is an object, but we can still use primitive datatypes like int and float. Thus, Python hides the object nature of these primitives to simplify code. Python is multi-paradigm (like C++). Global functions are allowed. Static methods are also allowed in Python. Smalltalk is considered a 100% pure OOP language Note: Python > Java > C++ > C
  • 7.
    WWW.PRIMECODING.IN MASSIVE SUCCESS RATE "TransformYour Interview Opportunity into an Offer Letter and Make Your Parents Proud!" In-depth Technical Mock Crack coding challenges with real experts. HR & Managerial Prep Master behavioral questions and impress Cognizant Interviewer. Full Interview Simulation Ace both technical and HR in one session. Resume Review Identify and fix weaknesses for a standout CV. Personalized Feedback & Expert Guidance Tailored improvement tips to boost success. www.primecoding.in
  • 8.
    INHERITANCE Inheritance is amechanism in which a subclass extends all the properties of the superclass. extends keyword is used. It provides code reusability. We can't access the private members of a class through inheritance. Method overriding is only possible through inheritance. class Employee { float salary = 40000; } class Programmer extends Employee { int bonus = 10000; public static void main(String args[]) { Programmer p = new Programmer(); System.out.println("Programmer salary is: " + p.salary); System.out.println("Bonus of Programmer is: " + p.bonus); } } class Employee: def __init__(self): self.salary = 40000 class Programmer(Employee): def __init__(self): super().__init__() # Call parent class constructor self.bonus = 10000 # Creating an object of Programmer class p = Programmer() print("Programmer salary is:", p.salary) print("Bonus of Programmer is:", p.bonus) Output: Programmer salary is: 40000.0 Bonus of Programmer is: 10000
  • 9.
    Java does notsupport multiple inheritance through classes because whenever a sub-class wants to inherit the property of two or more superclasses that have the same method, the Java compiler can't decide which class method it should inherit. Then there might be a chance of memory duplication. Java supports multiple inheritance using interface. Python support multiple inheritance. It uses the Method Resolution Order (MRO) to decide which method to execute when multiple parent classes have the same method.
  • 10.
    class GrandParent { voidgreet() { System.out.println("Hello from Grandparent!"); } } class Parent extends GrandParent { void display() { System.out.println("This is the Parent class."); } } class Child extends Parent { void show() { System.out.println("This is the Child class."); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.greet(); //Hello from Grandparent obj.display(); //This is the Parent class obj.show(); //This is the Child class } } Multilevel Heritance class GrandParent: def greet(self): print("Hello from Grandparent!") class Parent(GrandParent): def display(self): print("This is the Parent class.") class Child(Parent): def show(self): print("This is the Child class.") obj = Child() obj.greet() obj.display() obj.show()
  • 11.
    class Parent { voiddisplay() { System.out.println("This is the Parent class."); } } class Child1 extends Parent { void show1() { System.out.println("This is Child1 class."); } } class Child2 extends Parent { void show2() { System.out.println("This is Child2 class."); } } public class Main { public static void main(String[] args) { Child1 obj1 = new Child1(); Child2 obj2 = new Child2(); obj1.display(); //This is the Parent class obj1.show1(); //This is Child1 class obj2.display(); //This is the Parent class obj2.show2(); //This is Child2 class } } Hierarchical Heritance class Parent: def display(self): print("This is the Parent class.") class Child1(Parent): def show1(self): print("This is Child1 class.") class Child2(Parent): def show2(self): print("This is Child2 class.") # Creating objects of Child1 and Child2 obj1 = Child1() obj2 = Child2() obj1.display() obj1.show1() obj2.display() obj2.show2()
  • 12.
    Polymorphism is whenone task is performed by different ways. 1. Compile-time polymorphism: A polymorphism which exists at the time of compilation. Example: Method Overloading Whenever a class contains more than one method with the same name but having different numbers or different types of parameters, it is called method overloading. Polymorphism class A { void add() { int a = 10, b = 20, c; c = a + b; System.out.println(c); } void add(int a, int b) { int c = a + b; System.out.println(c); } void add(int a, double b) { double c = a + b; System.out.println(c); } public static void main(String[] args) { A obj = new A(); obj.add(); // Output: 30 obj.add(20, 30); // Output: 50 obj.add(10, 20.5); // Output: 30.5 } } class A: def add(self, a=10, b=20): print(a + b) obj = A() obj.add() # Output: 30 obj.add(20, 30) # Output: 50 obj.add(10, 20.5) # Output: 30.5 Python does not support method overloading in the way Java does (where multiple methods with the same name but different parameters exist). Instead, we can simulate method overloading using default arguments.
  • 13.
    2.Compile-time polymorphism: A polymorphismwhich exists at the time of execution of the program. Example: Method Overriding Whenever the subclass & superclass have the same method (name, return type, parameter), it is called method overriding. class shape { void draw() { System.out.println("I can't say shape type"); } } class square extends shape { void draw() { System.out.println("square shape"); } } class Test { public static void main(String[] args) { square s = new square(); s.draw(); // square shape } } class Shape: def draw(self): print("I can't say shape type") class Square(Shape): def draw(self): print("square shape") # Creating an object of Square class s = Square() s.draw() // square shape
  • 14.
    Encapsulation Encapsulation is themechanism of binding the data & the code acting on the data into a single unit. Example: A Java class Steps to achieve Encapsulation: Declare the variable of the class as private. Declare public setter & getter methods to modify & view the variable's value. Data Hiding: In encapsulation, the variables of a class will be hidden from other classes & can be accessed only through the methods of their current class. This is the concept of data hiding. class Employee { private int salary; // This is data hiding public void setter(int x) { salary = x; } public int getter() { return salary; } } class Test { public static void main(String[] args) { Employee e = new Employee(); e.setter(100000); System.out.println(e.getter()); //100000 } } class Employee: def __init__(self): self.__salary = 0 # Private variable (data hiding) def set_salary(self, x): self.__salary = x # Getter method def get_salary(self): return self.__salary # Test class if __name__ == "__main__": e = Employee() e.set_salary(100000) print(e.get_salary()) #100000
  • 15.
    Abstraction Abstraction is hidingthe implementation details & showing the functionalities. Its advantages are security & enhancement. Here enhancement means, for example, a calculator is coded in C++ which makes it slower. Now, we have changed it to Python. But the user sees the same button unaware of this change. Abstraction can be achieved by: Abstract class Interface In Java, abstract class is a class that is declared with abstract keyword. We can not create object for abstract class, but we can create its reference variable. It may or may not contain abstract methods It can contain abstract and non-abstract methods To use the abstract class, we have to create its subclass If a class contains partial implementation(no body), then we should declare a class as abstract
  • 16.
    Abstract Method: A methodthat contains the abstract keyword at the time of declaration is called an abstract method It can only be used in abstract classes & interface It does not contain any body or implementation An abstract method must be overridden in a subclass, otherwise, the subclass will also become abstract class Whenever an action is common but implementation is different, we should use an abstract class abstract class Animal { public abstract void sound(); // Abstract method } class Dog extends Animal { public void sound() { System.out.println("Dogs bark"); } } class Cat extends Animal { public void sound() { System.out.println("Cats meow"); } } class Test { public static void main(String[] args) { Dog d = new Dog(); d.sound(); // Calls Dog's sound() method Cat c = new Cat(); c.sound(); // Calls Cat's sound() method } } from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass # Abstract method class Dog(Animal): def sound(self): print("Dogs bark") class Cat(Animal): def sound(self): print("Cats meow") # Testing the subclasses d = Dog() d.sound() # Calls Dog's sound() method c = Cat() c.sound() # Calls Cat's sound() method
  • 17.
    In Java ,Interface is just like a class that contains only abstract methods .To achieve an interface, we use the implements keyword. An interface is a contract that defines a set of methods without providing any implementation. Interface methods are by default public and abstract. Interface variables are by default public, static, and final. Interface methods must be overridden inside the implementing classes. An interface is nothing but a bridge between a client and a developer. Interfaces help achieve abstraction and multiple inheritance in Java. interface Drawable { void draw(); } class Rectangle implements Drawable { public void draw() { System.out.println("Drawing rectangle"); } } class Circle implements Drawable { public void draw() { System.out.println("Drawing circle"); } } class TestInterface { public static void main(String args[]) { Drawable d = new Circle(); d.draw(); // drawing circle } }
  • 18.