Basic Java Syntax

Question:
Here’s some basic Java syntax, with explanations:

Answer:
OBJECTS, CLASSES and INSTANCES

A Java object is a container that holds variables and functions (Java programmers call functions “methods”). Since (references to) Java objects can be stored in variables, Java objects can also hold other Java objects.

There are two types of Java objects: arrays and class instances.

A class is a declaration that describes the members (i.e. methods and variables) its instances will contain. For example:

 class Employee {      //some instance variables      private double salary;      private String name;      // default printer method      public void println() {         System.out.println(“Name: ” + name);         System.out.println(“Salary: ” + salary);      }      // constructor initializes variable members      public Employee(String n, double s) {         salary = s;         name = n;      }      // two selector methods and a mutator method      public String getName() { return name; }      public double getSalary() { return salary; }      public void setSalary(double amt) { salary = amt; }      // etc.   }
When we declare objects to be instances of the class Employee, we call new to allocate heap space for the objects, then invoke the Employee constructor to initialize the member variables:
 Employee smith = new Employee(“Smith”, 30000),            jones = new Employee(“Jones”, 20000);
If we reassign Smith:
 smith = null;
and there are no other references to the original object bound to Smith, this object becomes garbage. When heap space runs low, Java’s garbage collector will automatically be called to recycle all garbage.

METHOD INVOCATION

While a method can freely refer to any other method or variable in the same instance, only methods and variables declared “public” can be accessed by methods in other instances. Since several instances of the same class can exist simultaneously, referencing a public method or variable from outside an instance requires qualifying the reference with a reference to the instance. For example:

 smith.setSalary(100000);
invokes Smith’s public setSalary() method, while
   jones.setSalary(10000);
invokes jones’ setSalary() method. But the scope of
smith.salary
includes only members of the Employee class.

INHERITANCE

Through inheritance, objects can have generalizations and specializations. For example, in addition to the methods and variables common to all employees, a secretary might also contain variables indicating typing speed and position:

 class Secretary extends Employee {            private int wpm; // typing speed in words/minute      private String position = “secretary”;      public void println() {         super.println();  // calls Employee.println()         System.out.println(“Position: ” + position);         System.out.println(“Typing speed: ” + wpm);      }      public String getPosition() { return position; }      // etc.   }
Managers too are employees (although they sometimes seem to forget). Their distinguishing features might include a private secretary:
  class Manager extends Employee {            private Secretary secretary; // objects can contain objects!      private String position = “manager”;      public void println() {         super.println();  // calls Employee.println()         System.out.println(“Position: ” + position);         System.out.println(“Secretary: ” + secretary.getName());      }      public String getPosition() { return position; }      public Secretary getSecretary() { return secretary; }      public void setSecretary(Secretary sec) { secretary = sec; }      // etc.   }

GENERALIZATION AND SPECIALIZATION

Suppose we represent companies by objects:

 class Company { … }
Inside we might declare an array of N employees:
 Employee[] staff = new Employee[N];
Filling this array with various types of employees requires no explicit casts:
staff[0] = new Secretary(…);   staff[1] = new Programmer(…);   staff[2] = new Manager(…);   // etc.
But specializing from an employee does require an explicit cast, which should only be done after the programmer verifies the specialization is valid:
 if (staff[0] instanceof Secretary && staff[2] instanceof Manager)      (manager)staff[2].setSecretary((Secretary)staff[0]);

DYNAMIC BINDING

Like C++ virtual functions, Java methods are dynamically bound. Each call to println() in the following “for” loop automatically invokes the appropriate specialized println() method:

for(int i = 0; i < N; i++)      staff[i].println();  // prints name, salary, position, etc.
This statement compiles because the Employee base class contains a default println() method. The absence of a default getPosition() method in the Employee base class prevents compilation of the statement:
for(int i = 0; i < N; i++)       if (staff[i].getPosition().equals("manager"))         managerCount += 1;
To correct this problem we can add an abstract getPosition() method to the Employee base class:
abstract class Employee {      // …      public abstract String getPosition();  // no body   }
The parallel construct in C++ is a pure virtual function:
class Employee {      public:         virtual String getPosition() = 0;  // no body!      // etc.   }

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job