Java Principles + OOP

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

Can we have a non-abstract method inside an interface?

From java 8 and onwards, you can have a non-abstract method in an interface, but previously not allowed.

What is the 5 objects oriented design principle from SOLID

S for Single Responsibility Principle O for Open closed design principle L for Liskov substitution principle I for Interface segregation principle D for Dependency inversion principle

What is serialization?

Serialization is a process of writing the state of an object into a byte stream

What is hash-collision in Hashtable and how it is handled in Java?

Two different keys with the same hash value is known as hash-collision. Two different entries will be kept in a single hash bucket to avoid the collision.

Can we override a non-static method as static in Java?

Yes, but it should not be private or final.

Can we change the return type of method to subclass while overriding?

Yes, but only from java 5 and onwards, known as 'covariant method overriding'

What is the difference between ArrayList and LinkedList?

ArrayList uses dynamic array, not efficient for manipulation because lot of shifting required, better to store and fetch data LinkedList uses doubly linked list, efficient for computation, better to manipulate data

What are the main differences between an abstract class and interface in Java?

Differences: 1. abstract class is a class, an interface is an interface, extending an abstract class you cannot extend another class, but you can implement multiple inheritance with interfaces 2. you cannot create a non-abstract method in an interface, every method in an interface is by default abstract, a class that doesn't contain any abstract can be made abstract 3. interface is better suited for type declaration, abstract class is more suited for code reuse 4. abstract classes can contain non-abstract methods but interfaces must have all abstract methods, but from java 8 and onward, interfaces can also contain static and default methods which are non-abstract 5. abstract classes are slightly faster than interface because an interface involves a search before calling any overridden method in Java 6. if you add a new method in the existing interface, it breaks all its implementation

What is composition?

Holding the reference of the other class within some other class

Static vs Dynamic binding

In static binding, type of object is determined at compile time, whereas dynamic binding determines type at runtime

What is inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behavior of another object of another class. It represents IS-A relationship. It is used for Code Reusability and Method Overriding.

Is there any difference between nested classes and inner classes?

Inner classes are non-static nested classes

Why are string objects immutable in java?

Java uses the concept of a string literal, suppose there are 5 reference variables, all refer to one object "mru", if one reference variable changes the value of the object, it will be affected to all the other reference variables

Can we change the argument list of an overriding method?

No

Can you override a static method?

No you cannot override the static method, because they are part of a class not an object.

Can a class extend more than one interface in java

No, Java doesn't support multiple inheritance for classes

Can we overload or override the main method in Java?

No, because its a static method

Can we override the final method in Java?

No, final is meant to prevent method overriding

Can you override a private method in Java?

No, since the private method is only accessible and visible inside the class they are declared, its not possible to override them in the subclass.

Can we override a static method in Java?

No, you cannot override a static method because its not bound to an object.

Can we prevent overriding a method without using the final modifier?

yes, you can prevent method overriding in java using the final modifier.

What are the major differences between Java and C++?

1. C++ supports pointers, whereas Java doesn't 2. At compilation time, Java Source code converts into byte code, the interpreter executes this byte code at run time and gives output, java is interpreted for the most part and is platform independent. C++ is run and compiled using a compiler which converts source code into machine level languages, so c++ is platform dependent. 3. Java uses a compiler and interpreter, C++ only uses a compiler 4. C++ supports operator overloading and multiple inheritance but java does not 5. Thread support is built in for Java, but not in C++ 6. Exception and Auto Garbage Collector handling in Java is different because no destructors in Java 7. Java is pass-by-value

What is the difference between Comparable and Comparator?

Comparable provides only one sort of sequence, provides one method named compareTo(), found in java.lang, if we implement Comparable interface, actual class is modified Comparator provides multiple sort of sequences, provides one method named compare(), it is found in java.util, actual class is not modified

What is method overriding in OOP or Java?

Concept based on polymorphism which allows a programmer to create two methods with the same name and method signature on interface which has various implementations that calls the method at runtime depending on the object. Rules: 1. you can only override a method in a sub class 2. name and signature must be the same in parent and subclass 3. cannot override private, static and final method in Java 4. it uses dynamic binding in java

What is an interface in Java? What is the real user of an interface?

Interface supports OOP abstraction, cannot define any method inside interface, all methods inside the interface must be abstract, allows multiple inheritance in java, which makes it possible for a class to become two different things such as Canvas and EventListener, interfaces are key in API Design, and its main use is to facilitate polymorphism

What is a Factory Method?

It offers an interface for creating an instance of a class, with its subclasses deciding which class instantiate.

What is the default method of Java 8?

JDK 8 managed to provide common functionality related to lambda expressions and Stream API without breaking all the clients which implement their interfaces?

What is a JVM?

JVM is an acronym for Java Virtual Machine, which is an abstract machine which provides the runtime environment in which java bytecode can be executed.

Can we make a class both final and abstract at the same time?

No, because they are exactly the opposite of each other. A final class in Java cannot be extended and you cannot use an abstract class without extending and make it a concrete class.

What is the difference between Polymorphism, overloading, and overriding

Polymorphism is the main concept between overloading and overriding ,overloading is compiled time, overriding is during runtime

What is method overloading in OOP or Java?

Programming concepts when you declare two methods of the same name but different method signatures, example: System.out.println() which can take many different parameters of different data types

What is runtime polymorphism?

Runtime polymorphism is a process in which a call to an overridden method is resolved at runtime rather then compile time.

What problem is solved by Strategy pattern in Java?

Strategy pattern allows you to introduce new algorithm or new strategy without changing the code which uses that algorithm. For example, Collections.sort() sorts the list of the object uses the Strategy pattern to compare objects, since every object uses different comparison strategy you can compare various objects differently without changing the sort method.

What is the difference between StringBuffer and StringBuilder?

StringBuffer is synchronized whereas StringBuilder is not synchronized

What is a Singleton Class?

The Singleton pattern ensures that a class has only one instance and ensures access to the instance through the application

Which OOP concept is the Decorator Design Pattern based upon?

The decorator pattern takes advantage of composition to provide new features without modifying the original class, you can extend the functionality of a class at runtime based on the customer's request e.g. Sandwich 1. decorator must be of the same type of object, can be done by implementing the interface of the object or extending an abstract class of the original class 2. Decorator pattern is a good example of Open Closed Design Principles 3. adds new functionality before or after delegating the task to the original object 4. purpose is to add functionality at runtime 5. Disadvantage: adds a lot of small classes in the code base

When to use Singleton design pattern in Java?

When you just need one instance of a class and wants that to be globally available, example is an Enum/Class for Color

Can an interface extend more than one interface in Java?

Yes

Can we override a method which throws runtime exception without throws clause?

Yes, there is no restriction on unchecked exception while overriding.

Can we overload a static method in Java?

Yes, you can declare as many static methods of the same name as long as all of them have different method signatures.

What is an abstract class in Java?

a class which is incomplete, you cannot create an instance of an abstract class, but you can define default behavior ensured of a client of that class, you must use the keyword "extend" to implement their abstract methods, used in conjunction with the factory method pattern

What is an applet?

a small java program that runs inside the browser and generates dynamic contents

What is the difference between abstraction and polymorphism in Java?

abstraction generalizes the concept and polymorphism allows you to use different implementation without changing your code

What is the difference between abstraction and encapsulation?

abstraction hides implementation details, encapsulation wraps code and data into a single unit

What is the difference between Association, Aggregation, and Composition in OOP?

association - when an object is related to another object, which has two forms aggregation and composition aggregation - the related object can survive individually composition - related object cannot survive individually Example: the city is an aggregation of people, but is the composition of body parts

What is the difference between composition and inheritance in OOP?

both allow you to reuse code, composition allows the class to get an additional feature at runtime, but inheritance is static

How do you create an immutable class in Java?

create a final class that have final data members

What is the difference between Decorator, Proxy, and Adapter pattern in Java?

decorator - provides additional functionality without touching the class proxy - provides access control Adapter - used to make two incompatible interfaces work together

What is the purpose of synchronized block?

is used to lock an object for any shared resource

Is Java a pure object oriented language?

java is not, because there are many things you can do without objects (static methods)

What is the difference between notify() and notifyAll()

notify() is used to unblock one waiting thread, notifyAll() method is used to unblock all the threads in a waiting state

What is abstraction?

process of hiding implementation details and showing only the functionality to the user

What is deserialization?

process of reconstructing the object from the serialized state

What is the difference between State and Strategy Pattern?

state pattern is used to do something specific depending upon state while strategy allows you to switch between algorithms without changing the code which uses it, Vending machine (State), encryption (Strategy)

What is the static keyword in Java?

static variable belongs to a Java Class, while non-static variables belong to objects. One static variable is shared between all objects in Java, which means in a multithreading environment access to the static variable must be synchronized so it is generally not recommended. Rules: 1. you cannot use a non-static variable inside a static method 2. faster, because they use static binding at compile time 3. static fields are initialized at class loading in java rather than during object creation 4. cannot override a static method

What is the difference between checked exception and unchecked exception?

the classes that extend throwable class are checked during compile time, but an unchecked exception will be known at compile-time

How do you call superclass version of an overriding method in sub class?

use the super keyword. example: toString -> super.toString()

What is the difference between wait() and sleep()?

wait method is defined in the object class and releases the lock the sleep method is defined in the thread class, doesn't release the lock

What does join() method?

waits for a thread to die, causes the currently running threads to stop executing until the thread joins with completes its task

What is method hiding in java?

when you declare two static methods with the same name and signature in both superclass and subclass so they hide each other

transient keyword

will not be serialized

Can you have virtual functions in Java?

yes, all functions in java are virtual by default.

Can we make a class abstract without an abstract method

yes, just add the abstract keyword on the class definition.


Ensembles d'études connexes

Global Issues: Achieving Sustainable Development

View Set

AG - Chapter 17.3 -Elections & Voting - Section 3 - Influences on Voters

View Set

Ohio Pre-Licensing Insurance Quiz Questions

View Set