Abstraction

abstraction means hiding lower-level details and exposing only the essential and relevant details to the users.

Encapsulation

The term encapsulation refers to combining data and associated functions as a single unit. All the data (fields) required for the methods in the class are available inside the class itself. In other words, the class encapsulates its fields and methods together.

Abstraction vs Encapsulation

encapsulation combines data (fields) and logically-related operations (methods). abstraction hides internal implementation level details and exposes only the relevant details of the class to the users. abstraction is achieved through  encapsulation.

Inheritance

Inheritance is also called an “is-a” relationship.

Inheritance is a reusability mechanism . Creating hierarchical relationships between related classes. The abstract and common properties are provided in the superclass, which is available to the more specialized subclasses.

You use the super keyword to call base class methods.

Overloading is an example of static polymorphism (early binding) while overriding is an example of dynamic polymorphism (late binding).

You need to do an explicit cast for doing a downcast. A downcast may fail. So you can use the instanceof operator to see if a downcast is valid.

Polymorphism

Polymorphism means “several forms” of an entity.

Interpreting the same message (i.e., method call) with different meanings depending on the context.

Polymorphism can be of two forms: static and dynamic. When different forms of a single entity are resolved at compile time (early binding), such polymorphism is called static polymorphism. When different forms of a single entity are resolved during runtime (late binding), such polymorphism is called dynamic polymorphism. Overloading is an example of static polymorphism

Method Overloading

In Java, you can define multiple methods with the same name, if you provide different types of arguments, different numbers of arguments, or both, then you can define multiple methods with the same name. This feature is called method overloading.

Constructor Overloading

You can have overloaded constructors. You can call a constructor of the same class in another constructor using the this keyword.

A default constructor is useful for creating objects with a default initialization value.

you can have multiple constructors in a class—which is constructor overloading.

the compiler can do upcasts (e.g., byte to int) for the closest match, but it does not consider downcasts (e.g., int to byte or int to short, as in this case). Hence, the compiler does not find any matches and throws you an error.

Points to consider when overloading

• Overload resolution takes place entirely at compile time. 
You cannot overload methods with the methods differing in return types alone.
You cannot overload methods with the methods differing in exception specifications alone.
For overload resolution to succeed, you need to define methods such that the compiler finds one exact match. If the compiler finds no matches for your call or if the matching is ambiguous, the overload resolution fails and the compiler issues an error.

Class

A class encapsulates state (data) and behavior (operations) of an entity.
A “class” is a template (or blueprint) and an “object” is an instance of a class.

Static Import

Java 5 introduced a new feature—static import—that can be used to import the static members of the imported package or class.

import static java.lang.Math.PI;

You can also use wildcard character “*” to import all static members of a specified package of class.





Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.
Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -