Interfaces are hardcore contracts with
no method that can contain bodies .
Ok , We made an interface with abstract methods now what ?
We create another class that ” IMPLEMENTS ” that interface
note : Every method from the interface must be implemented or , you will obtain an error.
Let’s create two classes in this case to be able to see how we will receive different outputs from the main class
Good so we have an interface , 2 classes let’s now make our main class that will execute everything.
I wonder what are we going to get as an output ?
See , the difference . Utilizing interfaces are important concept in the world of developers and software engineers.
Since, the contract defining what a client can expect leaves the developer free to implement it any way they choose, as long as they uphold the contract.
Just like we can not only store primitive data values ,a nd objects to files. We can likewise add objects inside an array to a file .
Saving the state of the objects to that file. Which are inside the array.
We start by creating an array , and putting our objects inside the array.
Saving , our array to our file.
Output from saving our array to a file :
So how do we read our objects again from the file ? We deserialize our File that contains our array , and will be able to see once again our objects.
FileInputStream and ObjectInputStream are very powerful tools what are they ?FileInputStream: obtains input bytes from a file in a file system. What files are available depends on the host environment. and ObjectInputStream : deserializes primitive data and objects previously written using an ObjectOutputStream.
Now ! , lets conclude our program .
Output from Reading our array that’s inside our file :
It is possible to store objects just as easy as we can store primitive data values !!
Using ObjectOutputStream and ObjectInputStream we can save and load the state of an object to a file
However if we want to save object we must have a given class we must have a maker interface. An interface that has no field or methods. It can be serializable or Clonnable.
Know you might be asking what’s sterilization/ deserialization. ?
Well , Serialization is a mechanism of converting the state of an object into a byte stream .
Meanwhile ,Deseialization is the reverse process where the byte stream is used to persist the object.
Just remember !!! IF we want to save objects from a given class . The declaration must include the phrase : IMPLEMENTS SERIALIZABLE
Q : What’s the difference between static and public variables ?
A: A static variable belongs to the class rather than an instance of a class. A static method belongs to the class rather than an object of a class. While a public can be accessed from any other class.
Q : Differences between HashMaps and ArrayLists ?
A: HashMaps don’t have a specific order, Hashmaps are organized only thought their keys , and values. Moreover with ArrayLists you are able to get the position of an element with an index(which is based on its position)
Q: What is a Java class ?
A :A Java class is a blueprint that allows for objects to be created .
Q : What is a Java constructor ?
A: A java constructor allows us to initialize variables as soon as we create an object. We can also have two objects calling one constructor .
Q : What are Java attributes ?
A : It’s a field, typically public constant or public variable that can be access directly. Also described as data members, or the variables inside the class/objects
Q : What is Java Inheritance ?
A : Java Inheritance allows different classes to receive all PUBLIC variables and PUBLIC methods. By using the simple keyword ** extends ** . The classes inheriting are subclasses and the class providing is the Super class.
Q : How to overwrite methods that were inherited?
A: by simply re-writing the same method in your desired sub-class
Q : Why should you avoid the finalize() method in the Object class? What are some alternatives?
A: finalize() it is executed by JVM just before the object is being garbage collected. – Java specification will not assured the immediate execution of that finalize method if the object is unreachable – Java language does not specify what thread is the finalizer invoking for an object, so it depends on the implementation of the java vendor. Some vendors may implement it as part of garbage collection thread (or) some may implement it as separate thread. Because of this finalize behavior may vary. – If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates.
* Work Around * : You can tell the system to try to run pending finalizers immediately by calling the method System.runFinalization after a garbage collection
key words used : Garbage collection : The process in which java programs perform automatic memory management. When Java programs run on the JVM , objects are created on the heap. Which is a portion of memory dedicated to the program. The garbage collector deletes unused memory. – Java Vendor : Vendor is just the creator/maintainer of the . List of all current java vendors. JVM.https://en.wikipedia.org/wiki/List_of_Java_virtual_machines
“when you don’t create things, you become defined by your tastes rather than ability. your tastes only narrow & exclude people. so create.” ―Jonathan Gillette
Q : Can you change the contents of a final array as shown in the code snippet below?
A : Yes, indeed we can change the contents of the array even if they were marked as final . This is all due to arrays pointing to a particular start location in the memory. This memory address will not changed
Key words used : final : to define an entity that can only be assigned once. – Arrays: An array is a group of the same type variables that are referred to by a common name.
“Talk is cheap. Show me the code.” ― Linus Torvalds
Q : Explain the difference between an interface and an abstract class? When should you use one or the other?
– An interface is a completely “abstract class” that is used to group related methods with empty bodies. – An abstract class : It can have abstract and non-abstract methods (method with the body).
-Variables declared in a Java interface are by default final. An abstract class may contain non-final variables. – A java interface is implemented using the keyword “implements” , and abstract class can be extended using keyword “extends”. – A Interface Class can implement multiple interfaces , abstract class can inherit only one Abstract Class
Q : What is polymorphism? Can you give an example?
A : polymorphism is the ability while you’re programming to present the same interface for different underlying forms data types.
Example: in many languages, integers and floats are implicitly polymorphic since you can add, subtract, multiply.Regardless of the fact that the types are different.
Poly = Many : polygon =many-sided, polystyrene = many styrenes -morph = change or form : morphology = study of biological form, Morpheus = the greek god of dreams able to take any form.
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live” ― John Woods
Q : Can a Semaphore act as a mutex ?
A: A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa. https://www.geeksforgeeks.org/mutex-vs-semaphore/A semaphore is a generalized mutex. In lieu of single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers. The consumer and producer can work on different buffers at the same time.
semaphore can potentially act as a mutex if the number of permits it can give out is set to 1. However, one of the biggest differences is that a mutex is owned by the thread acquiring it, till the point, it releases it, whereas for a semaphore there’s no notion of ownership.
Terminology : mutex and semaphore are kernel resources that provide synchronization services.
Q : What is a java method ?
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name. Think of a method as a subprogram that acts on data and often returns a value.
Each method has its own name. When that name is encountered in a program, the execution of the program branches to the body of that method.
Why is this an important concept for java : Well, good programmers write in a modular fashion which allows for several programmers to work independently on separate concepts which can be assembled at a later date to create the entire project.
Q :How does an instance data value differ from a class data value in Java?
A: An instance data value is defined specifically for objects, while a class data value is defined for classes. An instance data value stores temporary values, while a class data value stores permanent values. An instance data value is defined in the object, while a class data value is…
Q : What is an instance variable ?
Instance variable is the variable declared inside a class, but outside a method:
Now this IronMan Class can be instantiated in other class to use these variables
Q : What’s difference between a public void and a public static void main?
publicis an access specifier defining that it can be accessed by any of the classes be it in the same project same/other class, or other project classes. staticmeans that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class. (Note: whenever you run program, do you need to make object of main method()? Think about it!). void means that the method has no return value. If the method returned an int you would write into instead of void. citation : Prapti Sanghavi, Programmer
This is an example of how to use methods in different classes.
Java methods are methods in a class used to perform actions.
They can either be static or public.
Static Methods can be accessed in a class without creating an object.
Below is an example on using a Static Method. I created a class called UsingClassMethods.
public class UsingClassMethods
{
Then I created a static method called testStaticMethod.
static void testStaticMethod()
{
Finally I call the static method testStaticMethod from the main method.
testStaticMethod();
Below is the hole code put together.
public class UsingClassMethods
{
//Since this method is static it can be accessed in this class without creating an object of this class
static void testStaticMethod()
{
System.out.println("This is called from a static method in UsingClassMethods\n");
}
public static void main(String[] args)
{
//This is called from a static method in this class, UsingClassMethods
testStaticMethod();
}
}
Writing comments are useful when you want to describe what a piece of code in you program does. This can make your code easier to understand for others who read your code or for yourself when you revisit your program after a long hiatus.
In JAVA, there are two types of comments:
Single-line comments
Multi-line comments
A single-line comment is used when your comment only spans one line. If you want to write a comment that spans multiple lines, then a multi-line comment should be the preferred choice.
Below is an example of a single-line comment. In this example, there are two single-line comments. One is placed on its own line and the other is placed inline with the print statement.
// this is a single-line comment
System.out.println("Hello World"); // another single-line comment
Below is what a multi-line comment looks like. A multi-line comment begins with the ‘/’ character followed…
Hello guys, let me introduced you with one of the topics I was struggling while in High School. The term is encapsulation , do not let the name intimidate you. We will break it apart, and make it digestible for anyone to understand it.
Encapsulation : Means Hiding the internal details or mechanics of how an object does something
* The need for encapsulation is to make sure our ” private data its safe * Since the only way that we will be able to access our data will be through methods * public = anyone can access the variable
//com.collabera.encapsulation;
// (Project name here)
class EncapsulationDemo{
// encapsulation = binding data with methods
// Private variables
private Integer moneyOwed;
// We make sure variables are always private
private String name;
Notice the private variables , We will sometimes encounter private data the the user shouldn’t necessarily have access to.
// Getter, and setter methods that will access our private variables.
public Integer getMoneyOwed() {
return moneyOwed;
}
public void setTotal(Integer moneyOwed) {
this.moneyOwed = moneyOwed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
The private variables are bonded with the getter , and setter methods.
By binding our private data to our methods we create a more secure program.
public class Ex {
public static void main(String[] args) {
EncapsulationDemo demoObject1 = new EncapsulationDemo();
demoObject1.setTotal(5);
demoObject1.setName("Dan");
System.out.println(demoObject1.getMoneyOwed());
System.out.println(demoObject1.getName());
}
}
To be able to call those private variables , we need to call our get method
Humanity greatest achievements have been done through technology. Let’s learn how to create, manipulate and make a difference in our future. Together we can achieve greater heights. By being part of, and implement our knowledge of technology on any field that we are passionate. Let’s work for a better future.