Java notes
Java notes
Instance variables are assigned value by default, even if you don’t set them explicitly.
Normally reference type and object type must be same but with polymorphism , reference type and object
type can be different.
With polymorphism, the reference type can be a superclass of the actual object type.
A non-public class can be subclassed only by classes in the same package as the class.
A final class means that it’s the end of the inheritance line. Nobody, ever, can extend a final
class.
The third issue is that if a class has only private constructors, it can’t be subclassed.
If you want to protect a specific method from being overridden, mark the method with the final modifier.
To avoid anyone from instantiating a class, use the abstract keyword before it . The compiler won’t let you
instantiate an abstract class.
A class that’s not abstract is called a concrete class.
you can abstract a method as well.An abstract method means the method must be overridden.
public abstract void eat();
If you declare an abstract method, you MUST mark the class abstract as well. You can’t have an abstract
method in a non-abstract class.
Abstract methods don’t have a body; they exist solely for polymorphism.
if you want to use a refeernce variable of child class to an object of child class whose reference is Parent
class then you have to type cast like:
Dog myDog= (Dog)animal; (animal is a Superclass reference)
You can call a method on an object only if the class of the reference variable has that method.
An "Interface" is a pure abstract class where all the methods are abstract. It helps us in avoid Deadly
Diamond of Death.
use Serializable interface if you want an object to be able to save its state to a file.
Important -> The call to super() must be the first statement in each constructor!
public <T extends Animal> void takeThing(ArrayList<T> list) -> using generic type which is not included in
the class declaration.
In generics extends means "extends or implements". So Animal in the above code can be both an
interface or a class.
The sort method of the objects only takes comparable objects. And comparable is an interface. So only
the object that implement comparable<T> can run .sort() method.
Comparable interface only has one method which is "int compareTo(T o)" where T is a generic.
There is another interface called Comparator<T> which has method "int compare(T o1,T o2);".
you can use it like "List.sort(comparator)" or "Collections.sort(List,comparator)". In this , the sort methods
uses the compare method of the comparator instead of the compare method of the comparable.
to use comparator. First create a class that implements the comparator<T> interface and make an object
of that. Then pass that object to the List.sort(Object_you_created)
# Lambda
If an interface has only one method to implement , it can be implemented as Lambda Expression.
# Object Equality:
Reference equality is the comparison of hashcodes and the object equality is the object’s .equals()
method.
most versions of Java assign a hashcode based on the object’s memory address on the heap, so no two
objects will have the same hashcode.
By default -> a.equals(b) => a.hashCode() == b.hashCode() . So by default the equals method just
compares the references of the objects.
Tip -> use TreeSort for the sorted Set. You can pass in a comparator too.