We make the data private and access them using getter and setter
The setter method can have more than one parameter but it usually isn’t advised to do so since that means we have to pass the same number of arguments every time
getter method returns the private data we want
We use extends to inherit a class. If it is default attributes, the super class and the child class must be in the same package. But we can either make it public(not advisable since violates the encapsulation) or private, and the two classes can be in different package.
When we are invoking the constructor in the child class explicitly, the parent constructor gets executed first and then the child one
To call the parent class constructor from the child class constructor. Has to be the first thing in the constructor
To call a method from a parent class. super.methodName([argument]); We use when overloaded
To call variable from parent class when there is a variable with similar name in the child class.
We cannot use super and this in the same constructor since they both have to be the first line in constructors.
We don’t have to use super unless we are overriding or trying to solve name space collision
Super is only used by the child constructor, and it can only be in a constructor
Super can only call immediate parent’s constructor
If we have only parameterized constructor in the parent class, we must have an explicit constructor in child class and also invoke the parent constructor explicitly using the super. If we have multiple constructors in the child class, each one should invoke parent constructor explicitly
But if we were to have multiple constructors in the parent class, it’s not a must to call the parameterized one using super if we have a default in there somewhere. But we can if we want to
We need the following for dynamic method calling
Method Overriding
Super Class Reference Variable
Use constant/final methods (they are not meant to be changed or tweaked so the parent is called)
Use static methods (since they are properties of the class and not the object)
Implicit casting: done automatically by the compiler, when we assign a smaller value to a wider data type
Explicit: We don’t really need to when it is from widening since the wide variable can hold the small one’s content
but in case of the narrowing we need it and the syntax goes like this
ex. int n = (int) doubleDataType;
for an already declared doubleDataType.
Object casting
Upcasting: This is just using super class reference on a child class
DownCasting: We can convert a child class object that was being referred to by a super class variable, to a fully object of the child class with the variable of that class.
ex.
Animals myAnimal = new Cats();//this is upcasting
myAnimal.noise();
Cats cat = (Cats) myAnimal;
cat.growl();//Animal class doesn't have growl method
Making our class constant/final
Some classes like, Scanner, String, and Math are not inheritable
Abstraction
We use the keyword on our class and method to make it abstract. If our abstract class has abstract methods, we are obligated to override them. Only the child class and the overriding class know the implementation of the abstract methods. But if they don’t, we don’t have to. Abstract classes can have normal methods, but concrete classes cannot have abstract methods.
Our abstract methods can have a return type but the overriding method must match the type
Abstract classes cannot be instantiated
It is an entity that is like class. Has things that classes have but is conceptual hence cannot be instantiated
We can use a variable of the interface to store objects that implement the interface
Interfaces are implemented like classes are extended. A class can implement from multiple interfaces. If a class was to implement two interfaces and the interfaces have attributes with the same signature, we have to use fully qualified accessing method (which is the static method accessing method)
Attributes
All attributes are public, static and final by default even if we don’t make them
Can be initialized only at declaration. And we use the interfaceName.attribute to get them.
Methods
Our methods cannot be used to initialize our attributes(only at declaration)
We only had abstract methods in interface until after java 8. There are three types
Abstract Method
Methods even without the keyword abstract are considered as abstract by default
Our abstract methods are instance methods since overriding is only allowed on instance methods
Static Method- we use the keyword static
default Method- we use the keyword default and it is instance method that can ta