OOP

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is a Class?

A class is a building block of Object Oriented Programs. It is a user-defined data type that contains the data members and member functions that operate on the data members. It is like a blueprint that objects of the class must follow.

What is a subclass?

A class that derives from another class is referred to as a subclass. A subclass inherits the properties of its ancestors or parent classes. For example, the class Bike is a subclass or a derivative of the Vehicle class.

What is a destructor?

A destructor is a method that is automatically called when the object is made of scope or destroyed. In C++, the destructor name is also the same as the class name but with the (~) tilde symbol as the prefix. In Python, the destructor is named __del__ In Java, the garbage collector automatically deletes the useless objects so there is no concept of destructor in Java.

What is pure virtual function?

A pure virtual function, also known as an abstract function is a member function that doesn't contain any statements. This function is defined in the derived class if needed.

What is a superclass?

A superclass is a class from which a subclass or child class is derived. Base class and parent class are other names for a superclass. For example, if Student is a class derived from the Person class, then the Person class will be referred to as the superclass.

What is an interface?

A unique class type known as an interface contains methods but not their definitions. Inside an interface, only method declaration is permitted. You cannot make objects using an interface. Instead, you must put that interface into use and specify the procedures for doing so.

What is the virtual function?

A virtual function is a function that is used to override a method of the parent class in the derived class. It is used to provide abstraction in a class. In Java, every public, non-static, and non-final method is a virtual function.

What are access modifiers? What is their significance in OOPs?

Access modifiersare special types of keywords that are used to specify or control the accessibility of entities like classes, methods, and so on. Private, Public, and Protected are examples of access specifiers or access modifiers.The key components of OOPs, encapsulation and data hiding, are largely achieved because of these access specifiers.

What is an Object?

An object is an instance of a class. Data members and methods of a class cannot be used directly. We need to create an object (or instance) of the class to use them. In simple terms, they are the actual world entities that have a state and behavior.

How is an abstract class different from an interface?

Both abstract classes and interfaces are special types of classes that just include the declaration of the methods, not their implementation. When an interface is implemented, the subclass is required to specify all of the interface's methods as well as their implementation. When an abstract class is inherited, however, the subclass is not required to supply the definition of the abstract method until and unless the subclass actually uses it. An interface can only have abstract methods, abstract can have both abstract and non-abstract methods. The interface has only static and final variables, abstract class can have final, non-final, static and non-static variables. An interface supports multiple inheritances, but an abstract class doesn't.

What are the different types of Polymorphism?

Compile-Time Polymorphism Is the type of polymorphism where the binding of the call to its code is done at the compile time. Method overloading or operator overloading are examples of compile-time polymorphism. Runtime Polymorphism It is the type of polymorphism where the actual implementation of the function is determined during the runtime or execution. Method overriding is an example of this method.

What are the various types of constructors in Java?

Default Constructor The default constructor is a constructor that doesn't take any arguments. It is a non-parameterized constructor that is automatically defined by the compiler when no explicit constructor definition is provided. It initializes the data members to their default values. Non-Parameterized Constructor It is a user-defined constructor having no arguments or parameters. Parameterized Constructor The constructors that take some arguments are known as parameterized constructors. Copy Constructor A copy constructor is a member function that initializes an object using another object of the same class.

What are the main features of OOPs?

Encapsulation Data Abstraction Polymorphism Inheritance

What is Encapsulation?

Encapsulation is the binding of data and methods that manipulate them into a single unit such that the sensitive data is hidden from the users: Data hiding: A language feature to restrict access to members of an object. For example, private and protected members in Java. Bundling of data and methods together: Data and methods that operate on that data are bundled together. For example, the data members and member methods that operate on them are wrapped into a single unit known as a class.

What do you understand by Garbage Collection in the OOPs world?

Garbage collection is a memory recovery technique included in programming languages like C# and Java. Java splices memory between the Syack and the Heap which automatically frees up memory space allocated to objects and methods that are no longer needed by the program.

What is method overriding?

It is a language feature that allows a subclass or child class to provide a specific implementation of a method which is already provided by one of its superclasses or parent classes.

What is Constructor?

It is used to initialize objects of the class. A constructor resembles an instance method but it's not a method as it doesn't have a return type. It generally is the method having the same name as the class but in some languages, it might differ. For example: In python, a constructor is named __init__. In C++ and Java, the constructor is named the same as the class name.

What is Abstraction?

It means showing only functionality and hiding internal implementation details from the user. Abstraction is implemented using classes and interfaces.

Is it always necessary to create objects from class?

No. If the base class includes non-static methods, an object must be constructed. But no objects need to be generated if the class includes static methods. In this instance, you can use the class name to directly call those static methods.

What is Object Oriented Programming (OOPs)?

Object-oriented programming (OOP) is a style of programming based on the concept of "objects". Some of the main features in OOPS include Classes, Objects, Data Abstraction, Encapsulation, Inheritance, and Polymorphism.

What is the difference between overloading and overriding?

Overloading is when two or more methods in the same class have the same name but different parameters. Overloading allows an entity to have numerous implementations of the same name. It is solved at compile-time. Overriding is where the child class will inherit a method from the parents class and will execute a different implement implementation. The technique of using the same method signature both the superclass and the child class is known as overriding. It is solved at run-time.

What are the types of variables in OOP?

Primitive Variables: It is used to represent primitive values like int, float, etc. Reference Variables: It is used to refer to objects in Java. Instance Variables: Variables whose value varied from object to object are instance variables. For every object, a separate copy of the instance variable is created. Instance variables are declared within the Class and outside any method/block/constructor Static variables: For static Variables, a single copy of the variable is created, and that copy is shared between every Class object. The static variable is created during class loading and destroyed at class unloading. Static variables can be accessed directly from the static and instance area. We are not required to perform initialization explicitly for static variables, and JVM will provide default values. Local Variables: Variables declared inside a method or block or constructor are local variables. Hence the scope of local variables is the same as the block's scope in which we declared that variable.

What different types of inheritance are there?

Single Inheritance: Child class derived directly from the base class. Multilevel Inheritance: A derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes. In Java, a class cannot directly access the grandparent's members. Hierarchical Inheritance: One class serves as a superclass (base class) for more than one subclass. Multiple Inheritance (Through Interfaces): One class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In java, we can achieve multiple inheritances only through Interfaces. Hybrid Inheritance (Through Interfaces) It is a mix of two or more of the above types of inheritance. Since java doesn't support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.

Why OOPs?

Some of the advantages of OOPS include Reusability, Data Redundancy, Code Maintenance, Security, Design Benefits, Easy Troubleshooting, Better Productivity, Polymorphism Flexibility, and Problem-solving.

What is Inheritance? What is its purpose?

The idea of inheritance is simple, a class is derived from another class and uses data and implementation of that other class. The class which is derived is called child or derived or subclass and the class from which the child class is derived is called parent or base or superclass. The main purpose of Inheritance is to increase code reusability. It is also used to achieve Runtime Polymorphism.

What is Polymorphism?

The word "Polymorphism" means having many forms. It can also be called the ability of a programming language to present the same interface for different primary data types. It is implemented via method overriding (runtime polymorphism) and method overloading (compile-time polymorphism). It explains the concept that different classes can be used with the same interface. Each of these classes can have its own implementation of the interface.

What is method overloading?

There is a concept where two or more methods can have the same name. But they should have different parameters, different numbers of parameters, different types, or both. These methods are known as overloaded methods and this feature is called method overloading.

Can we overload the constructor in a class?

We can overload the constructor in a class. In fact, the default constructor, parameterized constructor, and copy constructor are the overloaded forms of the constructor.

Is it possible to call the base class method without creating an instance?

Yes, we can possibly call the base class method without creating an instance in the following 3 cases: 1) If the method is static 2) Calling the inherited method 3) inside a derived class Calling the method using the base keyword from the sub-classes The most popular case is that of the static methods.

What is an abstract class?

n general terms, an abstract class is a class that is intended to be used for inheritance. It cannot be instantiated. An abstract class can consist of both abstract and non-abstract methods. In Java, an abstract class is declared with an abstract keyword.


Ensembles d'études connexes

SIE missed simulated exam questions (day before)

View Set

Relevant Costs for Decision Making

View Set