Let’s check out the Code import java.io.FileReader; import java.io.BufferedReader; import java.io.FileWriter; public class FileIO { public static void main( String[] args ){ String src = "example.txt"; //if this file was in a directory. Use forward slash '/' String dst = "exampleCopy.txt"; //Syntax style: try with resources //This will allow Java to call the close() method automatically try( FileReader fr = new FileReader(src); BufferedReader br = new BufferedReader(fr); FileWriter wrtier = new FileWriter(dst); ) { while (true) { String line = br.readLine(); if(line == null) { break; } else { wrtier.write(line + "\n"); } } } catch( Exception e ) { ...
Read more
Understanding through Code Bike.java (abstract class) package com.example.java.model; public abstract class Bike { public static final String HONDA = "Honda"; public static final String YAMAHA = "Yamaha"; private String name = HONDA; private String color = "Red"; private int gearOrNot = 1; public Bike(String name, String color, int gearOrNot) { this.name = name; this.color = color; this.gearOrNot = gearOrNot; } public String getName() { return name; } public String getColor() { return color; } public int getGearOrNot() { return gearOrNot; } public abstract String getLogo(); } Details.java package com.example.java; import java.util.List; import com.example.java.model.Bike; public interface Details { public void getDetails(List<Bike> ...
Read more
Interface In Object Oriented Programming, an interface is a contract. It defines a set of methods with specific signatures and any class that implements that interface must implement those methods. Example: List and Map Understanding through Code Details.java (You can understand the folder path through package) package com.example.java; import java.util.List; import com.example.java.model.Bike; public interface Details { public void getDetails(List<Bike> bikes); //we are not implementing the method here public void setColor(String color); } Bike.java package com.example.java.model; public class Bike { public static final String HONDA = "Honda"; public static final String YAMAHA = "Yamaha"; private String name = HONDA; private String color ...
Read more
Let’s Dive into the Code Vehicle.java file in folder path: K:\Extending Classes and Overriding Methods\com\example\java package com.example.java; import com.example.java.model.*; //This imports all the files in model folder import java.util.List; import java.util.ArrayList; public class Vehicle { public static void main( String[] args ){ List<Bike> bikes = new ArrayList<>(); //Instead of: //bikes.add(new Bike(Bike.HONDA, 0x2E0456, 1)); //We do this as Honda knows all the values: bikes.add(new Honda()); bikes.add(new Bike(Bike.YAMAHA, 0x2E0854, 1)); bikes.add(new Bike(Bike.YAMAHA, 0x2E0555, 1)); for(Bike b: bikes) { System.out.println(b.getName() +" " + b.getColor() + " " + b.getGearOrNot()); } } } Bike.java file in folder path: K:\Extending Classes and Overriding Methods\com\example\java\model package com.example.java.model; public ...
Read more
Inheritance It means that there is a relationship between multiple classes. This lets you inherit or extend functionality from one class to another. C++ supports multiple inheritance but Java supports only single inheritance. Each class can extend or inherit functionality from only one other class. This makes it easier to manage the code. Each class can extend only one direct superclass. However, Java allows classes to implement multiple interfaces. About Inheritance Inheritance relationship can be described as: Parent/Child i.e. Parent has functionality and Child inherits it. Base/Derived Superclass/Subclass (Terms used in Java) Superclass has original functionality Subclass is inheriting Superclass’s ...
Read more
Why use final / constant variable? Currently in Java, developers create constant variables i.e. variables whose value don’t change with the help of ‘final’ keyword. Such constant variables are named in all uppercase letters. The use of these variables makes the coding easier and the code robust. If you by mistake write incorrect value for constant variable then it is easy to fix as the change needs to be done only once in declaration. Bike.java file in folder path: K:\Final\model package model; public class Bike { public static final String HONDA = "Honda"; public static final String YAMAHA = "Yamaha"; private String ...
Read more
Let’s Jump to Code Vehicle.java file in folder path: K:\Constructor import model.Bike; import java.util.List; import java.util.ArrayList; public class Vehicle { public static void main( String[] args ){ List<Bike> bikes = new ArrayList<>(); // Bike bike1 = new Bike("Honda", 0x2E0456, 1); // bikes.add(bike1); // Bike bike2 = new Bike("Yamaha", 0x2E0854, 1); // bikes.add(bike2); // Bike bike3 = new Bike("Suzuki", 0x000000, 1); // bikes.add(bike3); //Below is fine tuned code but same as above: bikes.add(new Bike("Honda", 0x2E0456, 1)); bikes.add(new Bike("Yamaha", 0x2E0854, 1)); bikes.add(new Bike("Suzuki", 0x2E0555, 1)); //BELOW CODE GIVES ERROR AS DATA IS PRIVATE // for(Bike b: bikes) { // System.out.println(b.name +" " + ...
Read more
Let’s Start with Something Simple Vehicle.java file in folder path: K:\Instance Variable import model.Bike; import java.util.List; import java.util.ArrayList; public class Vehicle { public static void main( String[] args ){ List<Bike> bikes = new ArrayList<>(); Bike bike1 = new Bike(); bikes.add(bike1); Bike bike2 = new Bike(); bikes.add(bike2); Bike bike3 = new Bike(); bikes.add(bike3); for(Bike v : bikes) { System.out.println(v.name); } } } Bike.java file in folder path: K:\Instance Variable\model package model; public class Bike { public String name = "Honda"; public long color = 0x2E0456; public int gearOrNot = 1; } Execution with errors: javac Vehicle.java Vehicle.java:1: error: cannot access Bike import model.Bike; ...
Read more
Let’s see the Code import java.util.Scanner; public class Calculator { public static void main( String[] args ){ Calculator c = new Calculator(); c.calculate(); } protected void calculate() { InputHelper helper = new InputHelper(); String s1 = helper.getInput("Enter a number: "); String s2 = helper.getInput("Enter a number: "); String op = helper.getInput("Choose an operation (+ - * /): "); double result = 0; try { switch(op) { case "+": result = MathHelper.addValues(s1, s2); break; case "-": result = MathHelper.subtractValues(s1, s2); break; case "*": result = MathHelper.multiplyValues(s1, s2); break; case "/": result = MathHelper.divideValues(s1, s2); break; default: System.out.println("Invalid operation"); return; } System.out.println("The ...
Read more
What is Encapsulation? Packaging complex code or functions for ease of programming. Putting all code in static main is not good programming. You need to break the code according to logic. Encapsulation provides restriction over parts of code via public, private, & protected. These parts of code would hide complex code. A programmer skilled in particular area of Java can code that part and provide interface to use by other developers. In regards of data, encapsulation may involve hiding of data. Example of Encapsulation in Real World Take a washing machine. You need to be able to operate the washing ...
Read more