java methods

class Test { public void print(String word) { System.out.println(word); } } class Test2 { public void callPrintMethod() { Test test = new Test(); test.print(“Hello”); } } class Test { public static void print(String word) { System.out.println(word); } public static void main(String[] args) { print(“Hello!”); // OK Test.print(“Hello!”); // OK } } class Test2 { public…

Read More Java Instance and Static Methods

public void methodName() { // code to be executed } public void methodName(int num, String data) { // code to be executed } methodName() methodName(10, “dataString”); class Test { public int sumNumbers(int a, int b) { int sum = a + b; return sum; } public static void main(String[] args) { int num1 = 10;…

Read More Methods in Java

public class App { public static void main(String[] args) { // … } } public class App { public static void main(String[] args) { for(String s : args) { System.out.println(s); } } } public static void main(String[] args) public static void main(String []args)  public static void main(String args[]) public static void main(String… args) static public void main(String[] args) public static final void main(String[] args) final public static void main(String[] args)

Read More Java main method

class Car { public void start() { // … } public void start(int a) { // … } public void start(String str) { // … } } class Colour { public void printColour() System.out.println(“This is general colour”); } } class Blue extends Colour { public void printColour() System.out.println(“This is blue colour”); } }

Read More Method Overloading vs Method Overriding in Java

abstract class Vehicle {} public abstract methodA(); abstract class Vehicle { // … } class Test { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); // DOES NOT COMPILE } } abstract class Vehicle { abstract void move(); } class Car extends Vehicle { @Override public void move() { System.out.println(“The car is…

Read More Abstract Classes and Methods in Java