Fanatah Java Academy

Fanatah Java Academy

Share

15/08/2020

*INHERITANCE*

*BY FANATAH | UNDER - JAVA FOR COMPLETE BEGINNERS*

Inheritance is the process where one class acquires the properties (methods and fields) of another.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

*extends Keyword*

'extends' is the keyword used to inherit the properties of a class. The syntax of extends keyword is as follows:

*Syntax*

class Super { ..... ..... }
class Sub extends Super { ..... ..... }

*Sample Code*

Here is an example demonstrating Java inheritance. In this example, there are two classes namely Calculation and MyCalculation.

Using extends keyword, MyCalculation inherits the methods addition() and Subtraction() of the Calculation class.

You can copy and paste the following program in a file with name MyCalculation.java & run it...

*Example*

class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}

public class MyCalculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20,
b = 10;
MyCalculation demo = new MyCalculation();
demo.addition(a, b);
demo.subtraction(a, b); demo.multiplication(a, b);
}
}

After executing the program, it will produce the following;

*Output*

The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200

In the above program, when an object of *MyCalculation* class is created, a copy of the contents of the superclass is also created within it.
That is why, using the object of the subclass you were able to access the members of a superclass.

N.B A superclass object / reference variable can only access the members of the superclass, but a subclass object can access both subclass and supercalss members.
That is why it is better for you to generally create an object/reference variable of the subclass.

*Note* Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

*The super keyword*

The *super* keyword is similar to *this* keyword.
Here are the instances where the super keyword is used.

1. )
It is used to *differentiate the members* of superclass from the members of subclass, if they have same names.

2. )
It is used to *invoke the superclass* constructor from subclass.

*Differentiating the Members*

If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below.

super.variable
super.method();

08/07/2020

INTRODUCTION TO JAVA TERMINOLOGY :

by Fanatah -

The JDK.

Very soon we will need to run Java applications on our machine so we practically learn how to write our own programs.
For you to do that you need to download a java software kit called the JDK.

JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop java applications and applets. It physically exists. It contains JRE + development tools. (such as classes, interfaces & methods that help you build ur own programs).

Thes predefined built in resources of classes and interfaces come packaged neatly in folders called packages that are all stored in a larger folder called the API -Application Programming Interface.
To use them or borrow them for your own programs you must import them usung the keyword 'import'.

JDK has different versions released in different times by Oracle and that is the one that determines which java version you are using.
E.g JDK 1.8 means you are using and studying Java version 8 classes & interfaces.
JDK.1.11 means version 11.

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) etc. to complete the development of a Java Application.

It can be downloaded and installed seperately or comes as part of an IDE (integrated development kit) such as Netbeans, Eclipse or Intelij.

*NB If u dont have it already installed on your machine, please download it or just download and install Netbeans so that when write our 1st program you have the tools to run it.*

Photos from Fanatah Java Academy's post 08/07/2020

JAVA BASICS :

VARIABLES -

By Fanatah.

1. Variables

A variable provides us with named storage that our programs can manipulate.
Each variable in Java has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

You must declare all variables before they can be used.
The basic form of a variable declaration is shown here:
data_type variable = value;

Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.

Following are valid examples of variable declaration and initialization in Java:
int a, b, c;
// Declares three ints, a, b, and c.

int a = 10, b = 10;
// Example of initialization

double pi = 3.14159;
// declares and assigns a value of PI.

char a = 'a';
// the char variable a iis initialized with value 'a'

Constant: During the ex*****on of program, value of variable may change. A constant represents permanent data that never changes.

If you want use some value likes p=3.14159; no need to type every time instead you can simply define constant for p, following is the syntax for declaring constant.
Static final datatype ConstantName = value;

Example: static final float PI=3.14159;

Lets discuss any challenge on this

2. Data type

Every variable in Java has a data type. Data types specify the size and type of values that can be stored.
Data types in Java divided primarily in two tyeps:
Primitive(intrinsic) and Non-primitive.

Primitive types contains Integer, Floating points, Characters, Booleans And Non-primitive types contains Classes, Interface and Arrays.

Integer:This group includes byte, short, int and long, which are whole signed numbers.
Floating-point Numbers: This group includes float and double, which represent number with fraction precision.
Characters: This group includes char, which represents character set like letters and number
Boolean: This group includes Boolean, which is special type of representation of true or false value.

Some data types with their range and size:
byte: -128 to 127 (1 byte)

short: -32,768 to +32,767 (2 bytes)

int: -2,147,483,648 to +2,147,483,647 (4 bytes)

float: 3.4e-038 to 1.7e+0.38 (4 bytes)

double: 3.4e-038 to 1.7e+308 (8 bytes)

char : holds only a single character(2 bytes)

boolean : can take only true or false (1 bytes)

3. Variable scope

There are three kinds of variables in Java:

Local Variable:
1. A variable that is declared inside the method is called local variable.
2. Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block.
3. Access modifiers cannot be used for local variables.
4. Local variables are visible only within the declared method, constructor or block.
5. There is no default value for local variables so local variables should be declared and an initial value should be assigned before the first use.

Instance Variable
1. A variable that is declared inside the class but outside the method is called instance variable . It is not declared as static.
2. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
3. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.
4. Instance variables can be declared in class level before or after use.
5. Access modifiers can be given for instance variables.
6. Instance variables have default values. For numbers the default value is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.
7. Instance variables can be accessed directly by calling the variable name inside the class. However within static methods and different class ( when instance variables are given accessibility) should be called using the fully qualified name . ObjectReference.VariableName.

Class/static variables:
1. A variable that is declared as static is called static variable. It cannot be local.
2. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
3. There would only be one copy of each class variable per class, regardless of how many objects are created from it.
4. Static variables are stored in static memory.
5. Static variables are created when the program starts and destroyed when the program stops.
6. Visibility is similar to instance variables.
7. Static variables can be accessed by calling with the class name ClassName.VariableName.

Example
class A{
int data=50;
//instance variable

static int m=100;
//static variable

void method(){
int n=90;
//local variable
}
}//end of class A

Want your business to be the top-listed Computer & Electronics Service in Harare?
Click here to claim your Sponsored Listing.

Telephone

Address


102 Twentydales, Hatfield
Harare

Opening Hours

Monday 09:00 - 17:00
Tuesday 09:00 - 17:00
Wednesday 09:00 - 17:00
Thursday 09:00 - 17:00
Friday 09:00 - 17:00
Saturday 09:00 - 13:00