Java OOP Flashcards
Advantages of OOP
easy concept - faster than procedural-oriented programming - reusable code - keep important data hidden
Object & class
Object have a state (color , sex , height …)(attributes) and behaviour (swim , fly , dance…)(methods) , a class is the blueprint of object (instance of class)(example : dog , human, car).
An object is a variable of class
Note !!!! A class doesn t take any byte of memory.
3 Steps to create an object
Declaration
Instantiation
Initialization
how many public class can we have in a source file
we can have only one public class (the one with the same name of source file) , but multiple default classes
“this” keyword
we use “this” keyword in constructor or an instance method to differentiate local variable from instance variable if they have the same name.
Call one type of contructor ( default with this() or parametrized with this(param) ) from
other in a class. it is known as explicit constructor invocation.
public vs static method
public method need a new object instantiation but static access is direct without instantiation.
finalize() method
has this form :
protected void finalize(){
//code here
}
it is called before object destroyed by GC
protected is used to prevent access from outside class.This means u won t know when or if finalize() execute. if you re program ends before GC occurs , finalize() will not execute
Method overloading
have the same name but different parameter ( and also number of param )
Command line argument
it is the information that follows directly java program name in command line when it is executed.
($java program-name “…”)
Java Methods Variables Arguments (var-args)
typeName… parameterName
In the method declaration, you specify the type followed by an ellipsis (…). Only one variable-length parameter may be specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it.
Constructor
it is a special type of method which initialize an object when created or do some procedures
name of constructor is same as class
java create automatically a constructor even if u don t create it , it initialize all variables to 0.
Once u created a constructor , default constructor provided by java is not invoked.
concept of creating multiple constructor in same class is : constructor overloading.
constructor don t have return type . DON T EVEN USE VOID TYPE !!!!!!!!!!!!!!!!!!!!!
access modifiers can be used to set its visibility/accessibility.
Type of constructor
has 3 types :
default constructor VS No-args constructor: default is provided by compiler , but no args is defined manually by programmer and u can give edit values of variables or do more process
they both don t accept arguments
Parametrized constructor: constructor with one or more param
Java Inheritance
One of java oop concepts. it is when a class acquire properties ( attributes and methods ) of another class .
A subclass inherits properties of superclass by using extends keyword :
class Sub extends Super{}
Why we need inheritance ?
Code reusability
extensibility(extend functionnality of a class)
Method overriding require inheritance
Abstraction require inheritance
“super” keyword
“super” keyword is like “this” keyword which differenciate between the members of a subclass from a superclass if they have same names.
for var : super.variable
for method:super.method()
It is also used to invoke superclass constructor from subclass
default constructor: super()
constructor with param: super(values)
if a class inherited properties from another class, the subclass automatically acquire default constructor of super class.
Java inheritance (IS-A relationship) type
Java support only single inheritance ( doesn t support multiple inheritance) but can implements multiple interfaces.
Interface in java can extends multiple interfaces.
What is Aggregation in java ?
A relationship (HAS-A) between two classes where a class contains instance of another class(like binding a class to other in one way direction)
benefits : reusable code
Aggregation types
unidirectional : like above def
bidirectional : where each of class contains instance of other class
Polymorphism
is the ability of an object to take on many forms , allow us to perform multiple operations using the single name of any method (interface).
method overriding is one of polymorphism concepts
any object that can pass more than one IS-A test is polymorphic.
All java objects are polymorphic, because an object can pass IS-A with its own type and the class Object.
Polymorphism types
compile-time polymorphism: method overloading (static polymorphism).
runtime polymorphism: method overriding.
method overriding
occur when a subclass implement a parent class method based on its requirement (overriding it ) without changing name , type , return type.