Professional Documents
Culture Documents
VARIABLE
Is a location in the computer’s memory where a value can be stored for use by a program.
SYNTAX:
VARIABLE DECLARATION
WHAT IS AN ARRAY?
A special kind of object used to store a collection of variables, all of the same type.
An ARRAY is something like a list of variables but it handles the naming of the variables in a nice and compact way.
A single name for a collection of data values.
Creating an Arrays
3 ways to Use [] (Brackets) with an Array Name
ARRAY TERMINOLOGY
ARRAY LENGTH
Specified by the number in brackets when created with new
maximum number of elements the array can hold
storage is allocated whether or not the elements are assigned values
The length attribute is established in the declaration and cannot be changed unless the array is redeclared.
SUBSCRIPT RANGE
Array subscripts use zero-numbering
The first element has subscript 0
The second elemnt has subscript 1
Etc. –the nth element has subscript n-1
The last element has subscript length -1
For example: an int array with 4 elements
ACCESING ARRAY
MULTIDIMENSIONAL ARRAY
MULTIDIMENSIONAL ARRAYS
Multi-dimensional array is similar with an array however, the difference from single dimension is it can store more
values utilizing more index reference from the created array.
This is very helpful in storing multiple related data in a single array.
This application allows you to have more than 1 value per index however, the data can be accessed by a more
complex reference or we can call it as sub-index of an array.
This can be interpreted in a tabular manner.
Problem One
PROBLEM TWO
public class Professor{ private String name; public String getName() { return name;
}
public void setName(String name)
{
this.name = name
}
}
System.out.println(p.getName()
);
}
INHERITANCE
Inheritance is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that
share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.
class Mammal {
}
class Panda extendsMammal {
Polymorphism
Polymorphism is Java OOP concept lets programmers use the same word to mean different things in different
contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by
the code itself. The other form is method overriding. That’s when the different meanings are implied by the values
of the supplied variables.
How Polymorphism Work
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means
forms. So polymorphism means many forms.
⊳ Polymorphism in Java works by using a reference to a parent class to affect an object in the child class. We might
create a class called “horse” by extending the “animal” class. That class might also implement the “professional racing”
class. The “horse” class is “polymorphic,” since it inherits attributes of both the “animal” and “professional racing” class.
class Person {
void walk()
{
System.out.println(“CanRun….”);
}
}
class Employee extendsPerson {
void walk()
{
System.out.println(“Running Fast…”);
}
public static void main(String arg[]) {
Person p = new Employee(); //upcasting p.walk();
}
OBJECT
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, doggo etc. It can
be physical or logical (tangible and intangible).
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name,
color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current
pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying
the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented
programming.
CLASS
In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other
bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and
therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the
class of objects known as bicycles. A classis the blueprint from which individual objects are created.
class Bicycle {
class BicycleDemo {
public static void main(String[] args) {
// Invoke methods on
// those objects bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates();
ABSTRACTION
Abstraction means using simple things to represent complexity. We all know how to turn the TV on, but we don’t
need to know how it works in order to enjoy it. In Java, abstraction means simple things like objects, classes, and
variables represent more complex underlying code and data. This is important because it lets avoid repeating the
same work multiple times.
Java Classes
CLASS
class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or
methods that are common to all objects of one type.
CLASS CONTAINS
Instance of data- Instance variable in Java is used by Objects to store their states. Variables which are defined without
the STATIC keyword and are Outside any method declaration are Object specific and are known as instance variables.
⊳ Constructor(s)- Constructors are used to initialize the object’s state. Like methods, a constructor also contains
collection of statements(i.e. instructions) that are executed at time of Object creation.
⊳ Method(s)- A method is a collection of statements that perform some specific tasks and return the result to the caller.
⊳ A method can perform some specific tasks without returning anything.
⊳ Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class
which is different from languages like C, C++, and Python.
OBJECT
It is a basic unit of Object-Oriented Programming and represents the real-life entities.
⊳ A typical Java program creates many objects, which as you know, interact by invoking methods.
⊳ OBJECTS CLASSIFICATION
▸ PHYSICAL-OBJECT THAT WE CAN TOUCH (STUDENT, ROOM)
▸ CONCEPTUAL- OBJECTS WE CANNOT TOUCH BUT EXIST(STUDENT NUMBER, COURSE)
OBJECT INSTANTIATION
When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the
behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have
any number of instances.
CLASS DECLARATION
The syntax for declaring a class is:
<modifier> class ClassName {
//field, constructor, and method declarations
}
Generally, declaring a class can include the following components, in order:
There are several kinds of variables and these are the following:
⊳ Fields – member variables of a class
⊳ Local Variables – variables declared in a method or block of code
⊳ Parameters – variables used in method declarations.
⊳ Field declarations are composed of three components:
▸ Zero or more modifier, such as public or private
▸ The field’s (data) type
▸ The field’s name
<modifier> <type> <name> [= <default value>];
Declaring Member Variables
Access Modifier
The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change
the access level of fields, constructors, methods, and class by applying the access modifier on it. Access modifiers are
categorise into four;
⊳ Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
⊳ Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the
package. If you do not specify any access level, it will be the default.
⊳ Protected: The access level of a protected modifier is within the package and outside the package through child class.
If you do not make the child class, it cannot be accessed from outside the package.
⊳ Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class,
within the package and outside the package.
class A{
private A(){}//private constructor
void msg()
{System.out.println("Hello java");}
}
public class Simple{
public static void main(String args[]){
A obj=new A();//Compile Time Error
}}
package sample2;
import sample1.*;
class B{
public static void main(String args[]){
A obj = new A();//Compile Time Error
obj.msg();//Compile Time Error
}
}
package sample1;
public class A{
protected void msg(){
System.out.println("Hello");
package sample2;
import sample1.*;
class B extends A{
public static void main(String args[]){
B obj = new B();
obj.msg();
}
}
package sample1;
public class A{
public void msg(){
System.out.println("Hello");
package sample2;
import sample1.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Overriding
Method overriding is one of the way by which java achieve Run Time Polymorphism.
class A{
protected void msg(){
System.out.println("Hello java");
Final keyword
final keyword is used in different contexts. First of all, final is a non-access modifier applicable only to a variable, a
method or a class.
⊳ When a variable is declared with final keyword, its value can’t be modified, essentially, a constant.
⊳ This also means that you must initialize a final variable.
Final keyword
class Car{
final int speedlimit=90;
void run(){
speedlimit=400;
}
public static void main(String args[]){
Car Subaru =new Car();
Subaru.run();
}
}