OOP related questions

Ace your homework & exams now with Quizwiz!

Define a constructor?

Constructor 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. Constructor must have no return type.

How do you make a class singleton?

Singleton class means you can create only one object for the given class. You can create a singleton class by making its constructor as private, so that you can restrict the creation of the object. Provide a static method to get instance of the object, wherein you can handle the object creation inside the class only. - Make constructor as private. - Write a static method that has return type object of this singleton class.

What are principles of OOP?

Encapsulation Abstraction Inheritance Polymorphism

What is OOP?

Object-oriented programming. It is a programming language model in which programs are organized around object or data, rather than functions and logic. Objects can be defined with different data types that includes both data and functions.

Explain polymorphism

We have a parent class and a few child classes which inherit from it. polymorphism gives a way to use a class exactly like its parent so there's no confusion with mixing types. But each child class keeps its own methods as they are.This typically happens by defining a (parent) interface to be reused. It outlines a bunch of common methods. Then, each child class implements its own version of these methods. objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface. In C++, we have two functions with the same name called calculateSum, but one has int as argument data type, and another has float as data type. So this is an example of polymorphism, that when we pass int object into this calculateSum function, the program knows which function should run because of the data type Dynamic or Run time polymorphism is also known as method overriding in which call to an overridden function is resolved during run time, not at the compile time. It means having two or more methods with the same name,same signature but with different implementation.

What is a class

A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object.

Association, Aggregation and Composition in Java

Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. Java composition is achieved by using instance variables that refers to other objects. In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association. In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity. Aggregation implies a relationship where the child can exist independently of the parent. Composition implies a relationship where the child cannot exist independent of the parent. Type of Relationship: Aggregation relation is "has-a" and composition is "part-of" relation. Type of association: Composition is a strong Association whereas Aggregation is a weak Association.

What is an object?

An object has a state and behavior. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes.

Explain abstracction

Applying abstraction means that each object should only expose a high-level mechanism for using it.This mechanism should hide internal implementation details. It should only reveal operations relevant for the other objects. For example, you know how to use a coffee machine but don't know how it operates inside. In this way, programmers can keep develop the internal functions but not affecting users how to use the program or the coffee machine.

Define Destructor?

Destructor 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.

what is cohesive?

High cohesion is when you have a class that does a well defined job. Low cohesion is when a class does a lot of jobs that don't have much in common. In object oriented design, cohesion refers all about how a single class is designed. Cohesion is the Object Oriented principle most closely associated with making sure that a class is designed with a single, well-focused purpose. The advantages of high cohesion is that such classes are much easier to maintain (and less frequently changed) than classes with low cohesion. Another benefit of high cohesion is that classes with a well-focused purpose tend to be more reusable than other classes.

Would you choose inheritance vs composition when designing classes?

Inheritance and Composition (Is-a vs Has-a relationship) in Java Inheritance If subtypes fulfill the "is-a" condition and mainly provide additive functionality further down the classes hierarchy, then inheritance is the way to go. Method overriding is allowed as long as the overridden methods preserve the base type/subtype substitutability promoted by the Liskov Substitution Principle. Additionally, we should keep in mind that the subtypes inherit the base type's API, which is some cases may be overkill or merely undesirable. I.E., Person and Waitress. Waitress IS A Person. Composition Allows us to model objects that are made up of other objects, thus defining a "has-a" relationship between them. Furthermore, the composition is the strongest form of association, which means that the object(s) that compose or are contained by one object are destroyed too when that object is destroyed. I.E., Person and Job. Person HAS A Job. Therefore, choose between inheritance and composition when designing classes really depends on their relationship: IS A or HAS A.

What is coupling?

Low coupling and high cohesive makes the program easier to maintain. coupling - Tight couplingis when two things depends on one another, that is, changing one may have impact on another. In JavaScript, method and object is said to be tightly coupled. - Low Coupling. you can easily make changes to the internals of modules without worrying about their impact on other modules in the system

Explain Single responsibility principle

Principle: A class should have one and only one reason to change, meaning that a class should have only one job. "Do one thing and do it well". It makes my software easier to implement and prevents unexpected side-effects of future changes.

Explain Interface segregation principle

Principle: A client should never be forced to implement an interface that it doesn't use or clients shouldn't be forced to depend on methods they do not use. Do not add additional functionality to an existing interface by adding new methods.Instead, create a new interface and let your class implement multiple interfaces if needed.

Explain Dependency inversion principle

Principle: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. High-level modules, which provide complex logic, should be easily reusable and unaffected by changes in low-level modules, which provide utility features. To achieve that, you need to introduce an abstraction that decouples the high-level and low-level modules from each other.

Explain Liskov substitution principle

Principle: Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T. All this is stating is that every subclass/derived class should be substitutable for their base/parent class. The LSP is saying that subtypes should not break the contracts set by their parent types. In practical terms, this means that if a given function uses some object, then you should be able to replace that object with one of its subtypes without anything breaking. The principle defines that objects of a superclass shall be replaceable with objects of its subclasses without breaking the application. That requires the objects of your subclasses to behave in the same way as the objects of your superclass. An overridden method of a subclass needs to accept the same input parameter values as the method of the superclass. Similar rules apply to the return value of the method. The return value of a method of the subclass needs to comply with the same rules as the return value of the method of the superclass. - A derived object cannot expect users to obey a stronger pre-condition than its parent object expects. - A derived object may not guarantee a weaker post-condition than its parent object guarantees. - Derived objects must accept anything that a base class could accept and have behaviors/outputs that do not violate the constraints of the base class.

Explain Open/closed principle

Principle: Objects or entities should be open for extension, but closed for modification. This simply means that a class should be easily extendable without modifying the class itself. It tells you to write your code so that you will be able to add new functionality without changing the existing code. That prevents situations in which a change to one of your classes also requires you to adapt all depending classes. Polymorphic OCP Use interfaces instead of superclasses to allow different implementations which you can easily substitute without changing the code that uses them. The interfaces are closed for modifications, and you can provide new implementations to extend the functionality of your software.

What are SOLID Design Principles

Principles of Object Oriented Design, Robert C. Martin The intention of these principles is to make software designs more understandable, easier to maintain and easier to extend. SOLID is an acronym for 5 important design principles when doing OOP (Object Oriented Programming). These 5 principles were introduced by Robert C. Martin. S — Single responsibility principle O — Open/closed principle L — Liskov substitution principle I — Interface segregation principle D - Dependency inversion principle

Explain inheritance

We have two classes and one of them is the child class that derives from another parent class. The child class reuses all fields and methos of the parent class, and can also implement its own.

Explain Encapsulation

this is achieved when each object keeps its state private inside a class. Other objects don't have direct access to this state. Instead, they can only call a list of public methods. So, the object manages its own state via methods.


Related study sets

Chapter 6 Networking Optimization Problems

View Set

3.1 Functions and Function Notation

View Set

You and Me and Pornography: How Porn Affects Relationships

View Set

Ch. 2: The Basic Theory Using Demand and Supply

View Set