This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* OverriddenMethod Demo. | |
* | |
* Method overriding. In a class hierarchy when superclass and subclass have a method with the same name and same signature than the method in subclass then is set to be overriden method. | |
* It is the way of representing polymorphism. | |
* A superclass reference can refer to its subclass object, but a subclass reference cannot refer to superclass object. | |
*/ | |
class A { | |
void display() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Constructor Demo. | |
* | |
* - Constructor is syntactically similar to a method, but it lacks of return type. | |
* - Constructor will have the same name as class name in which is declared. | |
* - Contructors are invoked immediately after creation of the objects. | |
* | |
* There are two types of constructors: | |
* - default constructor; | |
* - parameterised constructor; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Control Flow Demo. | |
* | |
* Constructor call always starts from the top superclass down to subclasses. | |
*/ | |
class A { | |
// A(int x) { // It'll generate an error, as there is no default constructor defined. | |
A() { | |
// super(); // Invisible call which invoke superclass constructor (Object). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Super Demo. | |
* | |
* super() is used to access members of superclass. | |
*/ | |
class A { | |
int x = 1000; | |
void display() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Final Keyword Demo. | |
* | |
* This demo will generate 3 errors: | |
* - error: cannot inherit from final A | |
* - error: display() in B cannot override display() in A | |
* - error: cannot assign a value to final variable x | |
* | |
* Uses of final keyword can be used: | |
* - to prevent inheritance; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Abstract Class Demo. | |
* | |
* - A class which has at least one abstract method is called as abstract class. | |
* - Abstract class is a class which has no complete implementation. | |
* - Abstract class cannot be instantiated. | |
* - An abstract class can have abstract and concrete methods. | |
* | |
* Abstract method is a method which has no body or implementation. | |
* Concrete method is a method which has the body (e.g. closing curly brackets). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Interface Class Demo. | |
* | |
* - An interface is a specification of method signatures. | |
* - Interface is syntatically similar to class declaration, | |
* but it lacks of instance variables (no instance variables). | |
* - An interface can extend one or more interfaces at one time. | |
* - Interfaces cannot be instantiated, | |
* because they represent pure abstract concept. | |
* - Interface can only include abstract methods. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Imple.java | |
* | |
* - Interface can have variables which are public, static and final. | |
* | |
* Usage: | |
* javac Imple.java && java MainClass | |
*/ | |
interface A { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Student.java - Package Demo | |
* | |
* - A package acts as a container which holds .class files. | |
* - Packages avoid the namespace collisions between the classes. | |
* - Once a package is created, it can be re-used or imported to any source code. | |
* - A package can be created using a package statement: package package_name. | |
* | |
* Usage: | |
* javac -d . Student.java |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* StudenInfo.java Package Demo | |
* | |
* Access Specifiers: private, protected, public, default | |
* Same class: Yes (private), Yes (protected), Yes (public), Yes (default) | |
* Non-subclass (same package): No (private), Yes (protected), Yes (public), Yes (default) | |
* Sub class (same package): No (private), Yes (protected), Yes (public), Yes (default) | |
* Non-sub class (different package): No (private), No (protected), Yes (public), No (default) | |
* Sub class (different package): No (private), Yes (protected), Yes (public), No (default) | |
* See more: http://www.slideshare.net/ravikantsahu/packages-26353534 |