Skip to Main Menu

Java Notes

Table of Contents

Java

Java is an Object-Oriented language, can be coffee too, but in this case, we’re making notes on the programming language.

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

These are my simple notes for reference online.

Collection of objects that communicate via invoking each other’s methods

  • Object − Objects have states and behaviors. Example: A cat has states - color, name, breed as well as behavior such as wagging their tail, meowing, eating. An object is an instance of a class.
  • Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports.
  • Methods − A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • Instance Variables − Each object has its unique set of instance variables. An object’s state is created by the values assigned to these instance variables.

As a language that has the Object-Oriented feature, Java supports the following fundamental concepts −

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
  • Classes
  • Objects
  • Instance
  • Method
  • Message Parsing

Objects

Objects have states and behaviors.

A software object’s state is stored in fields and behavior is shown via methods.

Creating a object

Object is created from a class.

example

public class PuddyCat {
   public PuddyCat(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myPuddyCat
      PuddyCat myPuddyCat = new PuddyCat( "Mikala" );
   }
}

Class

Blueprint from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

A class is a software construct that defines the data (state) and methods (behavior) of the specific concrete objects that are subsequently constructed from that class.

Class Variable Types

  • Local variables − Variables defined inside methods, constructors or blocks are called local variables.
  • Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
  • Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.

Constructors

Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.

  1. Every class has a constructor.
  2. Rule of constructors is that they should have the same name as the class

example

public class PuddyCat {
   public PuddyCat() {
   }

   public PuddyCat(String name) {
      // This constructor has one parameter, name.
   }
}

Source Code Declaration Rules

  1. Public class name should be the name of the source file as well which should be appended by .java at the end. For example: the class name is public class PuddyCat{} then the source file should be as PuddyCat.java.

Inheritance

  1. Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another
  2. extends is the keyword used to inherit the properties of a class.

Polymorphism

  1. Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

Abstraction

  • Abstraction is a process of hiding the implementation details from the user - only the functionality will be provided to the user.

Encapsulation

  • Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
  • Ok here

Resources

Tutorials