Java Basics Part 2

PropellerAds

Java Basics Part 2:before starting this topic lets revise some Object-Oriented Programming Concepts

state and behavior:objects contain state and behavior

fields:Object’s State is stored in fields

methods:behaviour of software object is contained in the method ,in simple meaning method is nothing but a function which perform specific task on objects

encapsulation:Hiding internal data from the outside world, and accessing it only through publicly exposed methods

class:A blueprint for a software object is

superclass Subclass and Extends keyword:Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword

interface:A collection of methods with no implementation

package:A namespace that organizes classes and interfaces by functionality

API :Application Programming Interface

Java Variables:

java variables is fields which store value,There are three type of variables in java

1.Instance Variables:also know as Non-Static fields,these fields are unique to each instance of class,i.e each class has its own copy of Instance variables

2.Class Varaibles:Also know as Static Fields,These fields were declared Using Static modifier,there is always one copy of the class variables although class can be instantiated many times.

3.Local Variables:Local variables store temporary state inside a method

Java Parameter:Parameters are variables that provide extra information to a method;

DataTypes in Java:

Eight primitive data types supported by the Java programming language, These are given below

byte, short, int, long, float, double, boolean, char

java.lang.String class represent the char string, An array is a the object that holds a fixed number of values of a single type

Java Operators:

Simple Assignment Operator
= Simple assignment operator

Arithmetic Operators
+ Additive operator (also used for String concatenation)
– Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator

Unary Operators
+ Unary plus operator; indicates positive value (numbers are positive without this, however)
– Unary minus operator; negates an expression
++ Increment operator; increments a value by 1
— Decrement operator; decrements a value by 1
! Logical complement operator; inverts the value of a boolean

Equality and Relational Operators
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to

Conditional Operators
&& Conditional-AND
|| Conditional-OR
?: Ternary (shorthand for
if-then-else statement)

Type Comparison Operator
instanceof Compares an object to a specified type
Bitwise and Bit Shift Operators
~ Unary bitwise complement
<< Signed left shift >> Signed right shift
>>> Unsigned right shift
& Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR

 

Java Control Flow Statements

The if-then Statement:it is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.

if-then-else Statement:IT provides a secondary path of execution when an “if” clause evaluates to false. Unlike if-then and if-then-else

switch statement:It allows for any number of possible execution paths.

while and do-while statements :it continually execute a block of statements while a particular condition is true. The difference between do-while and whileis that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once

for statement :It provides a compact way to iterate over a range of values. It has two forms, one of which was designed for looping through collections and arrays

Declaring Classes In Java
Here is the basic structure of java class

class ChildClass extends MyParentClass implements ChildInterface {
// field, constructor, and
// method declarations
}
Member Variables In JAva
member varaibles in java can declared as below

public int noofchild;
it contain modifier such as public or private.the fields type and field name

Java Access modifier
Here is a list from highest access to lowest access:
public: Unlimited access to everyone and everything
protected: Unlimited access but more restrictive
default-access: Limited access to a finite amount of other classes
private: Extremely limited access: not accessible by anything whatsoever
To have a fifth access modifier between protected and default-access so that every class anywhere that wishes to extend it should have access to it, but not have access if they don’t want to extend it is redundant, because it already exists in the form of public abstract.

naming rules and conventions are used for method and class names

  • the first letter of a class name should be capitalized, and
  • the first (or only) word in a method name should be a verb.

 

Defining Methods in java
By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc
for example
getChildofParent();

Method declarations have six components, in order:
Modifiers—such as public, private, and others you will learn about later.
The return type—the data type of the value returned by the method, or void if the method does not return a value.
The method name—the rules for field names apply to method names as well, but the convention is a little different.
The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
The method body, enclosed between braces—the method’s code, including the declaration of local variables

Method Overloading
methods within a class can have the same name if they have different parameter lists passed as argument to function.Overloaded methods are differentiated by the number and the type of the arguments passed into the method

Constructors in JAva
When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods, but with some important differences.A constructor is similar to a method. Only it has no return type, and it has the same name as the class name

Java Objects
objects is the blueprint of class, we can create an object from a class

Parent parentOne = new Parent(12);

The new keyword is a Java operator that creates the object,The new operator is followed by a call to a constructor, which initializes the new object.The new operator returns a reference to the object it created
By Using we can access the variables of class and we can call the method of the class.

Java garbage collector
The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced.

Returning a Value from a Method

  1. we can use the return statement to return the value in the body of the method
  2. If you try to return a value from a method that is declared void, you will get a compiler error.
  3. Any method that is not declared void must contain a return statement with a corresponding return value, like this:
  4.          return returnValue;
  5. The data type of the return value must match the method’s declared return type; you can’t return an integer value from a method declared to return a boolean.

Use of this Keyword in java
The keyword this refers to the current object, that is an instance of a class, not to a class itself.this keyword can refer to any member of the current object from within an instance method or a constructor by using this.

Interfaces in java
An interface declaration can contain method signatures, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods.A class that implements an interface must implement all the methods declared in the interface.An interface name can be used anywhere a type can be used.

Polymorphism

  • A class inherits fields and methods from all its superclasses
  • A subclass can override methods that it inherits, or it can hide fields or methods that it inherits.
  • we can prevent a class from being subclassed by using the final keyword in the class’s declaration. Similarly, you can prevent a method from being overridden by subclasses by declaring it as a final method

abstract class

An abstract class can only be subclassed; it cannot be instantiated. An abstract class can contain abstract methods—methods that are declared but not implemented. Subclasses then provide the implementations for the abstract methods.

Keyword super
If your method overrides one of its superclass’s methods, you can invoke the overridden method through the use of the keyword super

Java Packages
A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces.

Package names are written in all lower case to avoid conflict with the names of classes or interfaces,class names should start with a capital.

for example
java.awt.Rectangle.

Rectangle class in the java.awt package