4 main principles of OOP
inheritance
A class can inherit methods and field member from another class. It allows the programmer to create similar entities without needing to redefine similar attributes over and over. For example, bird class inherit all things from animal class.
encapsulation
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition. Encapsulation is the hiding of data implementation by restricting access to accessors and mutators.
polymorphism
One name, many forms. 2 basic types of polymorphism: override & overload.
override: When a subclass method has the same name, parameters and return type as a method in a superclass but has a different implementation.
overload: In the same class, same name but different parameters.
//Example
class Animal() {
public void move(){
//walk
}
}
class Bird extends Animal{
public void move() {
//fly
}
}
class Fish extends Animal {
public void move() {
swim();
}
}
abstraction
Abstraction is the process of hiding all but the relevant information about a thing to make things less complex and more efficient for the user. For example, we don’t need to know how a clock works in order to use it to tell time. Abstraction lets you focus on what the thing does instead of how it does it
Difference between polymorphism and inheritance
Inheritance refers to_using the structure and behavior_of a superclass in a subclass.
Polymorphism refers to_changing the behavior_of a super class in the subclass. Polymorphism deals with how the program decides which methods it should use, depending on what type of thing it has by the runtime. For example, bird object has move its own move(), then when call move() using bird, it should use this one, not the move() in animal class.
Encapsulation vs Abstraction
Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of
encapsulation. Abstraction on the other hand means showing only the necessary details to the intended user.
==================================
S.L.I.O.D principles
S — Single responsibility principle
A class should have one and only one reason to change, meaning that a class should only have one job.
O — Open closed principle
Objects or entities should be open for extension, but closed for modification. Open for extension means that we should be able to add new features or components to the application without breaking existing code. Closed for modification means that we should not introduce breaking changes, to existing functionality, because that would force you to refactor a lot of existing code.
Classes should be extendible, but not modifiable. That means that adding a new field to a class is fine, but changing existing things are not. Other components on the program may depend on said field.
L — Liskov substitution principle
I — Interface segregation principle
D — Dependency Inversion principle
Class
Super
Access Superclass overriden Members: super.methodName(parameter);
Access super class constructor: super(parameter);
"this"
this.x = x
this(parameter) --> constructor
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
casting
Can only call type's method, not the subclass's method
Employee ed = new Lawyer();
ed.getHours()//its okay 'cause Employee has this method as well
ed.sue()//compile error
Need to convert.
Lawyer realEd = (Lawyer) ed;
ed.sue()//ok
((Lawyer) ed).sue() // shorter version
upcasting: treat as super class
downcasting: treat a super class type as its real subclass type; need to be specified.
public, private & protected
static vs instance method
static:
Static method(s) are associated to the class in which they reside
They are designed with aim to be shared among all Objects created from the same class.
instance:
Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class.
Every individual Object created from the class has its own copy of the instance method(s) of that class.
class vs object
class: blueprint. Creates a definition in Java of the common features (fields and methods) of all objects that are created from it. Classes are definitions, they are intangible and insubstantial.
object: An instance of a class. A tangible bit of data in your program. Always built from a class. We say, "myCar is an instance of Car", "the variable lastOrder is of type Order". "Here in the code I am instantiating an (object of type) Entity"
================================
Abstract Class | Interface |
---|---|
a group of related methods with empty bodies | |
extend only one abstract class | can implement multiple interfaces |
child method can be same or less restrictive visibility | exactly same visibility(public) |
can have protected, public and public abstract | method must be public abstract |
contains data member and constructor | no data member or constructor |
only complete member can be static | member cannot be static |
can have static, final or static final variables | only can have static final variables(default) |
have non-final variable | variables are final by default |
can extend from a class or a abstract class | extend only from a interface |
==========================================
Object class
Every class implicitly extends Object.
Equal method
public boolean equal(object o) {
if (o instance of Point) {
Point other = (Point) o;
return x == other.x && y == other.y;
}
return false;
}
Iterator
- Iterator<T>: must have hasNext(), next(), remove()
- Iterator<T> is an interface
- | Iterable | Iterator | | :--- | :--- | | Only one method to produce an iterator: iterator() | manages iteration over an iterable |