CS112 - Basic Concepts in Java (Objects and Classes)
CS112 - Basic Concepts in Java (Objects and Classes)
IN JAVA
OBJECTIVES:
1. Explain and use the basic object-
oriented concepts in your programs
► CLASS
► OBJECT
► ATTRIBUTE
► METHOD
► CONSTRUCTOR
OBJECTIVES:
2. Describe advanced object-oriented
concepts and apply them in
programming as well.
► PACKAGE ► INHERITANCE
► ESCAPSULATION ► POLYMORPHISM
► ABSTRACTION ► INTERFACE
OBJECTIVES:
Classes are categories, and objects are items within each category. All class
objects should have the basic class properties.
For example: In the real world, a specific cat is an object of the “cats” class. All
cats in the world share some characteristics from the same template such as
being a feline, having a tail, or being the coolest of all animals.
In Java, the “Cat” class is the blueprint from which all individual cats can be
generated that includes all cat characteristics, such as race, fur color, tail length,
eyes shape, etc.
So, for example, you cannot create a house from the cat class, because a house
must have certain characteristics — such as having a door, windows and a roof —
and none of these object properties can be found in the cat class.
Object
An object is an entity that has a state, behavior and identity with a
well-defined role in problem space. It is an actual instance of a
class. Thus, it is also known as an instance. It is created every time
you instantiate a class using the new keyword.
Java objects are very similar to the objects we can observe in the real world.
A cat, a lighter, a pen, or a car are all objects.
For example, a cat’s state includes its color, size, gender, and age, while its
behavior is sleeping, purring, meowing for food, or running around like crazy
at 4 AM.
IDENTITY
The identity is a characteristic used to uniquely identify that object – such as
a random ID number or an address in memory. Simpler objects like a lighter
may have only two states (on and off) and behaviors (turn on, turn off), but
they still have an identity (that item’s manufacturing ID, for example).
STATE
A Java object’s states are stored in fields that represent the individual
characteristics of that object. For example, in a first-person shooter video
game, a pistol with an eight-bullets clip has nine states in total: one for each
bullet (e.g. 8 bullets, 7 bullets, 5 bullets, etc.), plus another one when it’s
empty (0 bullets).
BEHAVIOR
The object’s behavior is exposed through methods that operate its internal
state. For example, the “shooting” behavior will change the state of the
pistol from “8 bullets'' to “7 bullets” and so forth every time the player shoots
with the gun.
Example:
public class Main { Output:
int x = 5;
5
public static void main(String[] args) { 5
Main myObj1 = new Main();
Main myObj2 = new Main();
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}
Attribute
An attribute refers to the data element of an object. It
stores information about the object. It is also known as a
data member, an instance variable, a property or a
data field. Going back to the student registration system
example, some attributes of a student entity include
name, student number and school level.
Variables that belong to an object are usually called attributes, but you might
also see them called “fields”.
Example:
public class Main { public class Main {
int x = 5; String Fname = “Kai”;
int y = 3; String Lname = “Jin;
} int age = “27”;
}
Method
A method describes the behavior of an object. It is also
called a function or a procedure. For example, possible
methods available for a student entity are enroll and
attend school.
A method is a block of code that performs a specific task.
public Main() {
x = 5; Create a class constructor for the Main class.
} Set the initial value for the class attribute x.
Example:
import java.util.Scanner;
Encapsulation
Encapsulation refers to the principle of hiding design or
implementation information that are not relevant to the
current object.
Encapsulation in Java is a powerful mechanism for storing the data members
and data methods of a class together. It is done in the form of a secure field
accessible by only the members of the same class.
Encapsulation in Java is the process by which data (variables) and the code
that acts upon them (methods) are integrated as a single unit. By
encapsulating a class's variables, other classes cannot access them, and only
the methods of the class can access them.
Consider a real-life example of a man driving a car. The man only knows
that pressing the accelerators will increase the speed of a car or applying
brakes will stop the car, but he does not know how on pressing the
accelerator the speed is actually increasing, he does not know about the
inner mechanism of the car or the implementation of the accelerator,
brakes, etc in the car. This is what abstraction is.
Abstract class in Java
A class which is declared as abstract is known as an abstract class. It can
have abstract and non-abstract methods. It needs to be extended and its
method implemented. It cannot be instantiated.
Points to Remember
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the
body of the method.
Abstract classes and Abstract methods :
Superhero
FlyingSuperhero UnderwaterSuperhero
Polymorphism
Polymorphism is the ability of an object to assume
may different forms. Literally, "poly" means many
while "morph" means form.
Why Polymorphism?
Polymorphism allows us to create consistent code.
When we decide a type of entity by its behaviour and not via attribute we
should define it as an interface.
Like a class, an interface can have methods and variables, but the methods
declared in an interface are by default abstract (only method signature, no
body).
• Interfaces specify what a class must do and not how. It is the blueprint of
the behaviour.
• Interface do not have constructor.
• Represent behaviour as interface unless every sub-type of the class is
guarantee to have that behaviour.
• An Interface is about capabilities like a Player may be an interface and
any class implementing Player must be able to (or must implement)
move(). So it specifies a set of methods that the class has to implement.
• If a class implements an interface and does not provide method bodies for
all functions specified in the interface, then the class must be declared
abstract.
• A Java library example is Comparator Interface. If a class implements this
interface, then it can be used to sort a collection.
interface {