Skip to content

Instantly share code, notes, and snippets.

View kenorb's full-sized avatar
💭
The Consciousness Has Shifted...The Awakening Has Begun

Rafal W. kenorb

💭
The Consciousness Has Shifted...The Awakening Has Begun
View GitHub Profile
@kenorb
kenorb / OverriddenMethod.java
Last active August 29, 2015 14:04
Overridden Method Demo in Java.
/*
* 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() {
@kenorb
kenorb / ConstructorDemo.java
Last active August 29, 2015 14:04
Constructor Demo in Java.
/*
* 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;
@kenorb
kenorb / ControlFlow.java
Last active August 29, 2015 14:04
ControlFlow Demo in Java.
/*
* 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).
@kenorb
kenorb / Super.java
Last active August 29, 2015 14:04
Super Demo in Java.
/*
* Super Demo.
*
* super() is used to access members of superclass.
*/
class A {
int x = 1000;
void display() {
@kenorb
kenorb / FinalDemo.java
Last active August 29, 2015 14:04
Final Keyword Demo in Java.
/*
* 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;
@kenorb
kenorb / AbstractClass.java
Last active August 29, 2015 14:04
Abstract Class Demo in Java.
/*
* 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).
@kenorb
kenorb / InterfaceClass.java
Last active August 29, 2015 14:04
Interface Class Demo.
/*
* 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.
/*
* Imple.java
*
* - Interface can have variables which are public, static and final.
*
* Usage:
* javac Imple.java && java MainClass
*/
interface A {
@kenorb
kenorb / Student.java
Last active August 29, 2015 14:04
Student.java - Package Demo
/*
* 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
@kenorb
kenorb / StudenInfo.java
Last active August 29, 2015 14:04
StudenInfo.java Package Demo
/*
* 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