- OOP is faster and easier to execute
- OPP provides a clear structure for the programs
- OPP helps to keep the Java code "DRY" (Don;t repeat yourself), makes the code easier to maintain, modify, and debug
- Create reusable applications with less code and shorter development time
- static method, means that it can be accessed without creating an object of the class
- public method, which can only be accessed by objects
- Used to initizlize objects
- The constructor is called when an object of a class is created.
- Access Modifiers
- Non-Access Modifuers
- public: class is accessible by any other class
- default: don't specify "public", class only accessible by classes in the same package
- private: code is only accessible within the declared class
- protected: The code is accessible in the same package and subclasses(inherent).
- Class: final, abstract; attributes,methods: final, static, abstract, transient synchronized, volatile
- final: attributes and methods cannot be overidden/modified
- static:Attributes and methods belongs to the class, rather than object
- abstract: only in abstract class, only in methods
- transient: Attributes and methods are skipped when serializing the object containing them
- synchronized: Methods can only be accessed by one thread at a time
- The value of an attribute is not cached thread-locally, and is always read from the "main memory"
- Built-in packages (packages from java API, inclding Java Development Ecnironment)
- User0defined packages
- Use package package-name in the begining of the file
- save as java file
- compile the package: javac -d . mypackage.java
- subclass(child) the class that inherits from another class
- superclass(parent) the class being inherited from
- 'final' can be used to prevent from the class being inherienteed
Seperate methoeds in multiple classes
- Nest classes(a class within another class)
- Group classes that blong together, code more readble and maintainable
- The inner class can be private or protected, 'private' can prevent from accessing from outer-class
- 'static' can be used for the inner class, it can be accessed without creating an objet of the outer class
- Access outer class from the inner class:
- Hiding certain details and show essential information to the user
- Abstract class: cannot be used to create objects, can accessed by inheriting
- Abstract method: only used in abstract class, has no body, body is provided by subclass
- An 'interface' is completely "abstract class" that is used to group related methoed with empty bodies. 2.'Why use Interface', achieve security, hide details and show necessary details of an object; support 'multiple inheritance'.