- Java is compiled first to a byte-code, after that it's interpreted by a java virtual machine (JVM)
- The fondamental OOP concepts:
- Encapsulation: Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class ( done thanks to the accessors: private, public, protected) . Encapsulation is thus referred as data hiding. Protection of the data is tightly controlled by an interface. The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
- Inheritence: it is the capability of a class to use the properties
and methods of another class while adding its own functionality. In OOP, we often organize classes in hierarchy to avoid duplication and reduce redundancy.
When we talk about inheritance the most commonly used keyword would be extends and implements. If a class inherits a method from its super class, then there is a
chance to override the method provided that it is not marked final.
The benefit of overriding is the ability to define a behavior that's
specific to the sub class type.
- Polymorphism: The word "polymorphism" means "many forms". Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. When a superclass instance is expected, it can be substituted by a subclass instance. A reference to a class may hold an instance of that class or an instance of one of its subclasses - it is called substitutability. With Polymorphism, the instance method that is invoked will be determined only at run-time by the actual class of the object, not by the type of the reference.
- main() is declared in java as public static void beacuse:
- Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application
- static - When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class.
- void - Java is platform independent language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system. If main method is declared as private then - Program will compile properly but at run-time it will give "Main method not public." error.
- Overriding vs Overloading:
- Overriding means creating a new set of method statements for the same method signature. If a derived class requires a different definition for an inherited method, then that method can be redefined in the derived class. An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.
- Overloading in Java can occur when two or more methods in the same class share the same name with at least one of this things is true:
- The number of parameters is different in each method
- The parameters types are different.
- Be aware because we are no longer speeking about overloading if :
- the tow methods have the same name but different return types
- the name of parameters are different.
- There are two ways to reuse the existing classes, namely, aggregation and inheritance. With composition (or aggregation), you create a new class, which is composed of existing classes. With inheritance, you create a new class, which is based on an existing class, with some modifications or extensions.
- Abstarct vs Interface:
- Abstract class is a normal class (containing fields, methods and even constructor (which will be accessed only by subclasses)). For a design purpose, the class should never be instantiated and then defined as abstract (even if it don't contain any abstract method). A class may define some abstract methods with no body specification and subclasses are forced to provide the method statements for their particular meaning. The class should then be explicitely declared abstract.
- Interfaces are similar to abstract classes but all methods are abstract and all properties (fields) are static final (Constant). Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. An interface is then defined as a contract of what the classes can do.
- Dynamic vs Static method binding:
- Dynamic binding or late-binding is the ability of a program
to resolve references to subclass methods at runtime. During the compilation, the compiler checks whether the
method exists and performs type check on the arguments and return type,
but does not know which piece of codes to execute at run-time. The problem is that the compiler is unable to check whether the type casting operator is
safe. It can only be checked during runtime (which throws a
ClassCastException
if the type check fails). - Static biding is used when the method is declared static (Class Methods). Even if the method is overriding in the subclass, the method used is usually depending on the class reference and is fixed at the compilation.
- Class Methods vs Instance Methods
- Class Methods are methods declared static, these methods can access class variables and class methods directly, but are not allowed to invoke instance variables neither instance methods directly (they must be accessed via object reference)
- Instance Methods are the non static methods. They can access class and instance methods and variables
- Class Variable vs Instance Variable
- Class variable is a static variable whose value is common to all instances and can be access directly by the Class reference.
- Instance Variable is specific to each new instance and is accessed via object reference.
- We use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final
. All methods in a final class are implicitly final.- A Java Object is considered immutable when its state cannot change after it is created.To create an Immutable Class, whose state cannot change once created, you need to make the class final and all its members final. It is also possible to make some fields non final and allow modifications only in the constructor.
- Checked vs Unchecked exceptions:
- Checked exceptions must be explicitly caught or propagated as described in Basic try-catch-finally Exception Handling. .
- Unchecked exceptions extend the java.lang.RuntimeException. They do not have to be caught or declared thrown
- In OO Design, it is desirable to design classes that are tightly encapsulated, loosely coupled and highly cohesive, so that the classes are easy to maintain and suitable for re-use.
- Coupling is the degree to which one class knows about another class (The better is to have loosely coupled classes where the communication is done through interfaces and contracts).
- Cohesion refers to the degree to which a class or method resists being broken down into smaller pieces. High degree of cohesion is desirable. Each class shall be designed to model a single entity with its focused set of responsibilities and perform a collection of closely related tasks.
- Collections API:
- Set :No duplication allowed and objects are organized with or without order
- Map: Duplication values allowed using different keys and objects can be organized with or without order
- List: Duplication allowed, objects expected to be organized in an order
- Arrays: Duplication allowed, objects expected to be organized in an order, and strongly typed to a particular object type, and lacks ability to resize.
- String vs StringBuilder vs StringBuffer:
- Use a String if the object value will not change because the string is immutable
- When the object value will be manipulated and changed by a single thread, use a StringBuilder because it is unsynchronized (so faster)
- If the object value may be modified by different thread, use a StringBuffer because it is synchronized (thread safe).
- Every thing in Java is pass-by-value.
- Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. try{ ... }catch(Exception e){ ... }finally{ ..... }
- http://www.tutorialspoint.com/java/java_encapsulation.htm
- http://www3.ntu.edu.sg/home/ehchua/programming
- http://www.fromdev.com/2012/02/java-interview-question-answer.html
- http://en.wikipedia.org/wiki/Information_hiding
Nice article. Can you plan some thing more formal. Like dividing that into all main sections related to J2SE.
ReplyDeleteThanks Zubair
DeleteThis comment has been removed by a blog administrator.
ReplyDelete