CS 3230 Flashcards
== and .equals()
Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. … In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects
public, protected, private
Broadly speaking, public means everyone is allowed to access, private means that only members of the same class are allowed to access, and protected means that members of subclasses are also allowed.
Instance variables
An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class. Access modifiers can be given to the instance variable.
Calling static methods
A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name
Method overriding
Method overriding, in object-oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. It allows for a specific type of polymorphism (subtyping).
Polymorphism
In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types
Access private data members of a superclass
To access the private members of the superclass you need to use setter and getter methods and call them using the subclass object
Constructor chaining
Constructor chaining is the process of calling one constructor from another constructor with respect to current object
Interfaces
In Java, an interface is an abstract type that contains a collection of methods and constant variables. It is one of the core concepts in Java and is used to achieve abstraction, polymorphism and multiple inheritances. We can implement an interface in a Java class by using the implements keyword
Determining what methods may called through an interface-typed variable
Methods inside interface must not be static, final, native or strictfp. All variables declared inside interface are implicitly public, static and final. All methods declared inside interfaces are implicitly public and abstract, even if you don’t use public or abstract keyword.
The impact of implementing an interface
When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract
wait(), notify(), and notifyAll()
The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object’s monitor. The notifyAll() method wakes up all threads that are waiting on that object’s monitor.
start() versus run()
So what is the difference between the start and run method? Main difference is that when program calls start() method a new Thread is created and code inside run() method is executed in new Thread while if you call run() method directly no new Thread is created and code inside run() will execute on current Thread.
monitors
The monitor is one of the ways to achieve Process synchronization. The monitor is supported by programming languages to achieve mutual exclusion between processes. … Only one process at a time can execute code inside monitors
Synchronized methods
Synchronized method is used to lock an object for any shared resource. When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.