Skip to content

Instantly share code, notes, and snippets.

@vikasverma787
Last active May 23, 2021 12:53
Show Gist options
  • Save vikasverma787/a232b9041d4ae815fafe9d7dcec0ecbe to your computer and use it in GitHub Desktop.
Save vikasverma787/a232b9041d4ae815fafe9d7dcec0ecbe to your computer and use it in GitHub Desktop.
* Inheritance 2. Polymorphism 3. Abstraction 4. Encaptulation
* Polymorphism --> Method OverLoading Method Overriding
* Method Overriding Rules:
a). public private protected modifier
class Parent
{
protected Parent get() throws IOException
{
return null;
}
}
public class Child extends Parent{
public Child get() throws FileNotFoundException // can't have narrow modifier from parent
{ // can have narrow exception in throws clause in child class but not greater
return null; //can have sub class return type in child class
}
public static String getId()
{
try
{
return "ss";
}finally {
return "kk";
}
}
------------------------------------------------------------------------
* Abstraction --> Abstraction is a process of hiding the implementation details from the user, only the functionality will be
provided to the user. In other words, the user will have the information on what the object does instead of how it does it.
------------------------------------------------------------------------
Encaptulation --> Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods)
together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed
only through the methods of their current class. Therefore, it is also known as data hiding.
---------------------------------------------------------------------------
Association --> Association is a relationship between two objects.
Aggregation --> 'Has A' one can exist without other eg. Car and wheel.
Composition --> Composition is a special form of Aggregation where the part cannot exist without the whole. Composition is a
strong Association.
----------------------------------------------------------------------------
What is Boxing and Unboxing?
Conversion of a primitive type to the corresponding reference type is called boxing, such as an int to a java.lang.Integer.
Conversion of the reference type to the corresponding primitive type is called unboxing, such as Byte to byte.
--------------------------------------------------------------------------------
Method overloading
Suppose, we have following two methods
private static void printSum(double N)
private static void printSum(Integer N)
When you invoke printSum(10), what’s get invoked? you may be expecting, printSum(Integer N) get’s called. But that’s not the case.
Method resolution Rules
The compiler attempts to locate the correct method without any boxing, unboxing, or vararg invocations.
This will find any method that would have been invoked under Java 1.4 rules
If the first pass fails, the compiler tries method resolution again, this time allowing boxing and unboxing
conversions. Methods with varargs are not considered in this pass
If the second pass fails, the compiler tries method resolution one last time, allowing boxing and unboxing,
and also considers vararg methods
-----------------------------------------------------------------
SOLID Principle:
S Single responsibility principle -> The single responsibility principle (SRP) asserts that a class or module should do one thing only.
O open/Closed principle -> The Open/Closed Principle states that code entities should be open for extension, but closed for
modification. (Extension/Modification)
L Liskov substitution principle (LSP) -> If you have a class, Animal, with a MakeNoise() method, then any subclass of
Animal should reasonably implement MakeNoise(). Cats should meow, dogs should bark, etc. What you wouldn't do is define a MuteMouse
class that throws IDontActuallyMakeNoiseException. This violates the LSP, and the argument would be that this class has no business
inheriting from Animal.
I is for Interface Segregation Principle -> The Interface Segregation Principle (ISP) says that you should favor many, smaller,
client-specific interfaces over one larger, more monolithic interface. In short, you don't want to force clients to depend on
things they don't actually need.
D is for Dependency Inversion -> The Dependency Inversion Principle (DIP) encourages you to write code that depends upon abstractions
rather than upon concrete details.
To visualize this in your day to day, go down to your local store and pay for something with a credit card. The clerk doesn't examine
your card and get out the "Visa Machine" after seeing that your card is a Visa. He just takes your card, whatever it is, and swipes it.
Both you and the clerk depend on the credit card abstraction without worrying about specifics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment