Introduction to Java programming

Introduction to Java programming. This tutorial explains the usage of the Java programming language. It also contains examples for standard programming tasks.

IDE

In this tutorial, I will use https://repl.it/ as my IDE, and then we will move to Android Studio

Write Code

when you create a new repl using the Java language you will see the following code:

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

run the previous code you should see "Hello world!" output on the right screen

Base Java language structure

Class

A class is a template that describes the data and behavior associated with an instance of that class.

A class is defined by the class keyword and must start with a capital letter. The body of a class is surrounded by {}.

class MyClass {

}

The data associated with a class is stored in variables the behavior associated with a class or object is implemented with methods.

A class is contained in a text file with the same name as the class plus the .java extension. It is also possible to define inner classes, these are classes defined within another class, in this case, you do not need a separate file for this class.

Object

An object is an instance of a class.

The object is the real element that has data and can perform actions. Each object is created based on the class definition. The class can be seen as the blueprint of an object, i.e., it describes how an object is created.

Inheritance

A class can be derived from another class. In this case, this class is called a subclass. Another common phrase is that a class extends another class.

Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class.

Important terminology:

  • Super Class: The class whose features are inherited is known as a superclass(or a base class or a parent class).

  • Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

Example: In the below example of inheritance, class Bicycle is a base class, class MountainBike is a derived class that extends Bicycle class and class Test is a driver class to run the program.

//Java program to illustrate the  
// concept of inheritance 
  
// base class 
class Bicycle  
{ 
    // the Bicycle class has two fields 
    public int gear; 
    public int speed; 
          
    // the Bicycle class has one constructor 
    public Bicycle(int gear, int speed) 
    { 
        this.gear = gear; 
        this.speed = speed; 
    } 
          
    // the Bicycle class has three methods 
    public void applyBrake(int decrement) 
    { 
        speed -= decrement; 
    } 
          
    public void speedUp(int increment) 
    { 
        speed += increment; 
    } 
      
    // toString() method to print info of Bicycle 
    public String toString()  
    { 
        return("No of gears are "+gear 
                +"\n"
                + "speed of bicycle is "+speed); 
    }  
} 
  
// derived class 
class MountainBike extends Bicycle  
{ 
      
    // the MountainBike subclass adds one more field 
    public int seatHeight; 
  
    // the MountainBike subclass has one constructor 
    public MountainBike(int gear,int speed, 
                        int startHeight) 
    { 
        // invoking base-class(Bicycle) constructor 
        super(gear, speed); 
        seatHeight = startHeight; 
    }  
          
    // the MountainBike subclass adds one more method 
    public void setHeight(int newValue) 
    { 
        seatHeight = newValue; 
    }  
      
    // overriding toString() method 
    // of Bicycle to print more info 
    @Override
    public String toString() 
    { 
        return (super.toString()+ 
                "\nseat height is "+seatHeight); 
    } 
      
} 
  
// driver class 
public class Test  
{ 
    public static void main(String args[])  
    { 
          
        MountainBike mb = new MountainBike(3, 100, 25); 
        System.out.println(mb.toString()); 
              
    } 
} 
No of gears are 3
speed of bicycle is 100
seat height is 25

How to use inheritance in Java

The keyword used for inheritance is extends

class derived-class extends base-class  
{  
   //methods and fields  
}  

Types of Inheritance in Java

Below are the different types of inheritance which are supported by Java.

Single Inheritance: In single inheritance, subclasses inherit the features of one superclass.


class one 
{ 
    public void print_geek() 
    { 
        System.out.println("Geeks"); 
    } 
} 
  
class two extends one 
{ 
    public void print_for() 
    { 
        System.out.println("for"); 
    } 
} 
// Driver class 
public class Main 
{ 
    public static void main(String[] args) 
    { 
        two g = new two(); 
        g.print_geek(); 
        g.print_for(); 
        g.print_geek(); 
    } 
} 
Geeks
for
Geeks

Java interfaces

What is an interface in Java?

An interface is a type similar to a class and is defined via the interface keyword. Interfaces are used to define the common behavior of implementing classes. If two classes implement the same interface, other code that works on the interface level can use objects of both classes.

Like a class an interface defines methods. Classes can implement one or several interfaces. A class that implements an interface must provide an implementation for all abstract methods defined in the interface.

public interface MyInterface {

        // constant definition
        String URL = "https://www.vogella.com/";

        // public abstract methods
        void test();
        void write(String s);

        // default method
        default String reserveString(String s){
            return new StringBuilder(s).reverse().toString();
        }
}

Implementing Interfaces

public class MyClassImpl implements MyInterface {

    @Override
    public void test() {
    }

    @Override
    public void write(String s) {
    }

    public static void main(String[] args) {
        MyClassImpl impl = new MyClassImpl();
        System.out.println(impl.reserveString("Lars Vogel"));
    }

}

Reference:

Last updated

Was this helpful?