TAKE NOTE THAT THIS IS NOT A .JAVA FILE BECAUSE IT WILL BE A PLAIN TEXT FILE. INSTEAD, JAVA WILL BE COMPILED VIA THE METHOD SHOWN IN THIS VIDEO.
TL;DW
Skip to steps 3 - 5 if "NppExec" is already installed.
Skip to step 5 to execute the Java file that is already in the desired directory.
Encapsulation
There are 4 core concepts in OOP:
The idea behind encapsulation is to ensure that
implementation details are not visible to users.
The variables of one class will be hidden from the other classes, accessible only through the methods of the current class. This is called data hiding.
To achieve encapsulation in Java, declare the class' variables
as private and provide public setter
and getter methods to modify and view the variables' values.
For example:
class BankAccount {This implementation hides the balance variable, enabling access to it only through the deposit method, which validates the amount to be deposited before modifying the variable.
In summary, encapsulation provides the following benefits: Inheritance
Inheritance is the process that enables one class to acquire the properties (methods and variables) of another. With inheritance, the information is placed in a more manageable, hierarchical order.
The class inheriting the properties of another is the subclass (also called derived class, or child class); the class whose properties are inherited is the superclass (base class, or parent class).
To inherit from a class, use the extends keyword.
This example shows how to have the class Dog to inherit from the class Animal.
When one class is inherited from another class, it inherits all of the superclass' non-private variables and methods.
Example:
As you can see, the Dog class inherits the legs variable from the Animal class.
We can now declare a Dog object and call the eat
method
of its superclass:
Constructors are not member methods, and so are not inherited by subclasses.
However, the constructor
of the superclass is called when the subclass is instantiated.
Example:
Result: (Animal.java) (Dog.java) (MyClass.java) | (A.java) (B.java) (Program.java)
Polymorphism
Polymorphism, which refers to the idea of "having many forms", occurs when there is a hierarchy of classes related to each other through
inheritance.
A call to a member method
will cause a different implementation to be executed, depending on the type of the object invoking the
method.
Here is an example: Dog and Cat are classes that inherit from the Animal class. Each class has its own implementation of the makeSound()
method.
As all Cat and Dog objects are Animal objects, we can do the following in main:
public static void main(String[] args) { We've created two reference variables of type Animal, and pointed them to the Cat and Dog objects.
Now, we can call the makeSound() methods.
As the reference variable a contains a Dog object, the makeSound()
method
of the Dog class will be called.
The same applies to the b variable.
Result: (Animal1.java) (Cat.java) (Dog1.java) (Program1.java)
Method Overriding
As we saw in the previous lesson, a subclass can define a behavior that's specific to the subclass type, meaning that a subclass can implement a parent class
method
based on its requirement.
This feature is known as
method overriding.
Example:
In the code above, the Cat class overrides the makeSound() method of its superclass Animal.
Rules for Method Overriding:
When methods have the same name, but different parameters, it is known as
method overloading.
This can be very useful when you need the same
method
functionality for different types of parameters.
The following example illustrates a
method
that returns the maximum of its two parameters.
The method
shown above will only work for parameters of type integer.
However, we might want to use it for doubles, as well. For that, you need to overload the max
method:
Now, our max method
will also work with doubles.
An overloaded method
must have a different argument
list; the parameters should differ in their type, number, or both.
Result: (Program2.java)
Abstraction
Data abstraction provides the outside world with essential information, in a process of representing essential features without including implementation details.
A good real-world example is a book. When you hear the term book, you don't know the exact specifics, such as the page count, the color, or the size, but you
understand the idea, or abstraction, of a book.
The concept of abstraction is that we focus on essential qualities, rather than the specific characteristics of one particular example.
In Java, abstraction is achieved using abstract classes and interfaces.
An abstract class is defined using the abstract keyword.
Abstract Class
For example, we can define our Animal class as abstract:
abstract class Animal { The makeSound method
is also abstract, as it has no implementation in the superclass.
We can inherit from the Animal class and define the makeSound()
method for the subclass:
Result: (Animal2.java) (Cat1.java) (Program3.java)
Interfaces
An interface is a completely
abstract class that contains only abstract methods.
Some specifications for interfaces:
An example of a simple interface:
interface Animal {Interfaces have the following properties:
Use the implement keyword to use an interface with your class.
interface Animal {Result: (Animal3.java) (Cat2.java) (Program4.java)
Type Casting
Assigning a value of one type to a variable of another type is known as Type Casting.
To cast a value to a specific type, place the type in parentheses and position it in front of the value.
Example:
The code above is casting the value 3.14 to an integer, with 3 as the resulting value.
Another example:
Result: (Program5.java)
Type Casting
For classes, there are two types of casting.
Upcasting
You can cast an instance of a subclass to its superclass.
Consider the following example, assuming that Cat is a subclass of Animal.
Java automatically upcasted the Cat type variable to the Animal type.
Downcasting
Casting an object of a superclass to its subclass is called downcasting.
Example:
This will try to cast the variable a to the Cat type and call its makeSound() method.
Why is upcasting automatic, and downcasting manual? Well, upcasting can never fail. But if you have a group of different Animals and want to downcast them all to a Cat, then there's a chance that some of these Animals are actually Dogs, so the process fails. Anonymous Classes
Anonymous classes are a way to extend the existing classes on the fly.
For example, consider having a class Machine:
When creating the Machine object, we can change the start method on the fly.
public static void main(String[] args) {After the constructor call, we have opened the curly braces and have overridden the start method's implementation on the fly.
The @Override annotation is used to make your code easier to understand, because it makes it more obvious when methods are overridden.The modification is applicable only to the current object, and not the class itself. So if we create another object of the class, the start method's implementation will be the one defined in the class.
class Machine {Result: (Machine.java) (Program6.java)
Inner Classes
Java supports nesting classes; a class can be a member of another class.
Creating an inner class is quite simple. Just write a class within a class. Unlike a class, an inner class can be private. Once you declare an inner class private,
it cannot be accessed from an object outside the class.
Example:
Result: (Robot.java) (Program7.java)
Comparing Objects
Remember that when you create objects, the variables store references to the objects.
So, when you compare objects using the equality testing operator (==), it actually compares the references and not the object values.
Example:
equals()
Each object has a predefined equals
method that
is used for semantical equality testing.
But, to make it work for our classes, we need to override it and check the conditions we need.
There is a simple and fast way of generating the equals()
method, other
than writing it manually.
Just right click in you class, go to Source → Generate hashCode() and equals()...
This will automatically create the necessary methods.
class Animal { The automatically generated hashCode()
method is used
to determine where to store the object internally. Whenever you implement equals, you MUST also implement hashCode.
We can run the test again, using the equals
method:
Result: (Animal4.java) (Program8.java)
Enums
An Enum is a special type used to define collections of constants.
Here is a simple Enum example:
Note that the values are comma-separated.
You can refer to the constants in the enum above with the dot syntax.
After declaring an Enum, we can check for the corresponding values with, for example, a switch statement.
Rank a = Rank.SOLDIER; You should always use Enums when a variable (especially a
method parameter)
can only take one out of a small set of possible values.
If you use Enums instead of integers (or String codes), you increase compile-time checking and
avoid errors from passing in invalid constants, and you document which values are legal to use.
Result: (Program9.java)
Java API
The Java API is a collection of classes and interfaces that have been written for you to use.
The Java API Documentation with all of the available APIs can be located on the Oracle
website at http://docs.oracle.com/javase/7/docs/api/
Once you locate the package you want to use, you need to import it into your code.
The package can be imported using the import keyword.
For example:
The awt package contains all of the classes for creating user interfaces and for painting graphics and images.
The wildcard character (*) is used to import all of the classes in the package. Object-Oriented Programming