The ability of an object to take many forms.

Photo by Nitty Ditty on Unsplash

We can create polymorphic objects when we have a parent-child relationship between classes or interfaces and their implementations.

Let’s say we have these classes


The main method has a mechanism to accept input when run from the terminal.

terminal

Run from terminal

Let’s say I have this class

public class Main {
public static void main(String[] args) {
System.out.println("hello, world!");
}
}

This code is saved in Main.java file under my desktop. In order to run it from the terminal, I will need to navigate with my terminal to my desktop and execute these commands:

javac Main.java — to compile the source code into java byte code.

java Main — to run without any arguments.

Run from the terminal with arguments

public class Main {
public static void main(String[] args) {…


Converting String to numeric values

To convert String data type to numeric data type we can use wrapper classes of numeric primitives.

public class Main {
public static void main(String[] args) {
String strNum = "12";
int num = Integer.parseInt(strNum);
System.out.println(num); // 12
}
}

We convert similarly for other numeric data types by using their wrapper class versions.

public class Main {
public static void main(String[] args) {
String strNum = "12av";
// NumberFormatException here
int num = Integer.parseInt(strNum);
System.out.println(num);
}
}

If we try to convert a string that is not a number(the content of the string is not a number), we will…


Abstraction focuses on what an object does instead of how it does it.

Photo by Paweł Czerwiński on Unsplash

Abstraction allows us to focus on what an object does instead of how it does. Abstraction is achieved by abstract methods. In java, abstract methods can be created in the abstract classes and interfaces.

  1. Abstract class.
  2. Interface.

Abstract class

  • An abstract class is a special class in Java that can have abstract methods.
  • We cannot initialize abstract class directly.
  • The abstract class can have regular methods and properties as well.
  • Abstract class extend another abstract class but implementation of abstract methods are not required.
  • The first non-abstract class that…


  1. Inheritance.
  2. Method overriding and variables shadowing.
  3. this and super keywords.

Inheritance

Inheritance is a process where one class can inherit visible properties and methods from another class — the parent-child relationship between two classes (or superclass and subclass).

// in Person.java file
public class Person {
public String name;
public String address;
public int age;

public void walk() {
System.out.println(name + " is walking.");
}
}// in Student.java file
public class Student extends Person {
public static void main(String[] args){
Student student = new Student();
student.name = "John Doe";
student.address = "101 Main St";
student.age = 22;
student.walk();
}
}
Student extends Person

In…


in Person.java file

Encapsulation is a data hiding mechanism. We achieve encapsulation by making our variables private and providing public getters and setters.

Why do we need to encapsulate? If the property has public access, the client code can have direct access and can assign any value. By encapsulating we have one layer where we can control what comes to our property in our setter method. In the above example, we can see how we are restricting negative age in the setAge method. Another example — let’s say we are creating a custom List data structure based on an array. The underlining array…


The final keyword can be used with variables, methods, and classes.

Photo by Maxim Zhgulev on Unsplash

The final keyword is a special specifier that you can put together with variable, method, and class declarations.

  1. Final variable.
  2. Final method.
  3. Final class.

Final variable

Let’s start with the variables. Once we assigned a value for the final variable there is no way we can reassign it.


static variables and methods belong to its class and not to a specific object.

  • object with reference john assigning the value for static variable address
  • object with reference smith to accessing the static variable and getting the value that was assigned by john. This example shows that static members are kind of global for all objects.
  • The correct way of using the static members is by class name.


Access modifiers are used to control access for variables, methods, and sometimes inner classes.

There are 4 access modifiers available:

public — accessible from everywhere. If we mark it as public it will be accessible from everywhere.

protected — accessible within the same package and child classes.

default — if we don't put anything it’s the default access modifier. It’s accessible within the same package only.

private — accessible within the same class only.

Let’s take a look into the examples with methods:


Objects are the main part of object-oriented programming. It’s crucial to know how they work.

When building software we move and manipulate with a lot of data. Data comes in different forms so we need a way to represent our data in our code. Java provides 8 core primitive data types.

byte       for whole numbers
short for whole numbers
int for whole numbers
long for whole numbers
float for floating numbers
double for floating numbers
char for single character(ACII table)
boolean logical - can be true or false

Using these primitives we can represent whole numbers, floating numbers, single characters…

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store