Basics of Java Programming and OOP
Aim
The main aim of this blog is to provide you the basic and most important concepts of Java Programming.
What Will You Need?
prerequisites for this blog is given below:
Java should be installed on your PC and IDE available on your PC (Notepad, NetBeans, Eclipse, etc. )
What Will You Learn?
After reading this blog you should be able to:
- Primitive types vs reference types
- Declaration of method
- Composition
- Static classes, methods, and variables
- Abstract classes, methods
- OOP(inheritance, polymorphism, abstraction, encapsulation)
- Interfaces in java
Outline
- Java — Class
- Java — Object
- Java — Method
- Java — Attributes
- Primitive types vs reference data types
- private vs public vs protected access specifiers in java
- Composition
- Static methods and variables
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Interface — java
- Conclusion
Java — Class
Let’s take an analogy to understand the concept of class. We want to drive a car and accelerate pedal to go fast, but what must happen before you can do this? Before you can drive a car, someone must design it. Before using any product, we must design it, how it works. In java class is like a template or blueprint.
// declaration of class
Public class Car {
}
Java — Object
We can’t drive a drawing of a car; we must build it from drawing before using it. Just like a car, we can’t drive a class; we must make an object for using the functionality of a class, such as an accelerator. We create an object of a class and we access the different features/functions of a class, which is defined in it.
// creating object of class
ClassName objectName=new ClassName();
Java — Method
When you press the pedal, we send a message to the car to perform a task — that is, make the car go faster. Similarly, you send messages to object — each method is called method call and tells a method of the object to perform a task.
Public void method_name(parameter list){
// method body
}
Java — Attributes
So far, we introduce the class, object, and method through car analogy. A car has many attributes like color, cylinder capacity, the number of doors and seats, current speed, etc. these attributes are part of engineering design. Similarly, an object has attributes that are carried with the object as it is used in the program. These attributes specified are part of an object’s class.
public class ClassName{
#declaration of variable
Data_type variable_name;
}
Primitive vs Reference Data Types
Two types of data are used in java: primitive and references types (non-primitive).
Primitive Data Types
Data types are defined by language. Primitive types variable can store exactly one value.
Example:
char, boolean, int, float, long, double, short, byte
The default value for each type is different e.g. boolean default is false and int default is 0.
Non-primitive Data Types
Data types are defined by the programmer.
A reference type variable is a reference to an object.
Other than the primitive types, all are reference types
The default value is ‘null’
Private Vs Public Vs Protected Access Specifiers in Java
We can use access modifiers with classes, methods or attributes
Public: accessible from anywhere.
Private: accessible from the same class only.
Protected: accessible within the same package
Composition
A class can have a reference to objects of the other classes as members. It is called composition or sometimes called has-a relationship.
For example, an object of a class AlarmClock needs to know the current time and a time when it is supposed to sound its alarm, so it is reasonable to include the two objects of Time objects as a member of AlarmClock object. Let’s see another example
// Date Class
public class Date{
int month;
int day;
int year;
}
// Employee Class
Public class Employee{
private String firstName;
private String lastName;
// Employee class with reference to other objects
private Date birthDate;
private Date hireDate;
}
// Main Class
public class EmployeeTest{
public static void main( String args[] ){
Date birth = new Date( 7, 24, 1949 );
Date hire = new Date( 3, 12, 1988 ); 10 11 12 13 }
Employee employee = new Employee( “Bob”, “Blue”, birth, hire );
// end main
}
Static Methods and Variables
‘static’ keyword can be used with methods or variables. Static members don’t belong to specific instances or objects. static methods or variables can be accessed by class name without using an object. A class can be static if it is inner class, inner classes will no be discussed in this article. Let’s see how to declare the static variables or methods
// declaration of class static variable
static data_type variable_name;
// declaration of class static method
static void myMethod()
{
System.out.Println(“this is static methods”)
}
Abstraction
Abstraction is the process of hiding the implementation detail from the user. Let suppose when we drive a car, we are not aware of what is doing inside when we press the pedal and we have no concern about the method implementation. The purpose of abstraction is to hide the irrelevant detail from the user.
Abstract classes and methods
An abstract class is declared with the ‘abstract’ keyword
Abstract methods are declared with the ‘abstract’ keyword
If a class has one or more abstract methods, then it is called an abstract class
// declaration of abstract class
public abstract class ClassName{
//abstract methods declaration
public abstract void method_name();
}
Encapsulation
Encapsulation is defined as wrapping up the data in a single unit. In encapsulation, variables of the class hidden from other classes, and can be accessed only through the methods of their current class. It is known as data hiding. To hide variables we use ‘private’ keyword and use ‘setter’ ‘getter’ method to update or access the variables of the class.
private data_type variable_name;
Inheritance
Inheritance is a mechanism in which one class requires the properties (attributes or methods) of another class. The class which requires the properties is called sub-class and the existing class is called super-class.
“extend” keyword is used to inherit the super-class with sub-class. “super” keyword is used to differentiate the superclass members from sub-class and invoke the superclass constructor form sub-class.
Let suppose there is a parent class person and every person in the world must have the name, age, height, color
//parent class
Public class Person{
Int age;
String name;
float height;
//common method
public void eat(){
System.out.println(“eat food”)
}
}
// sub-class 1
public class Student extends Person{
public void occupation(){
System.out.println(“Student ”)
System.out.println(super.age); // super keyword to access parent class attribute
}
}
// sub-class 2
public class Employee extends Person{
public void occupation(){
System.out.println(“Employee”);
System.out.println(super.age); // super keyword to access parent class attribute
}
}
// main class
public class main{
public static void main(String args[]){
Employee emp=new Employee();
Student std=new Student();
emp.occupation();
emp.eat(); // parent class method accessed from child class
std.occupation();
}
Note: Java doesn’t allow multiple inheritances.
Polymorphism
Polymorphism means ‘many forms’ and polymorphism allows us to perform a single action in different ways. Let suppose a class shape has a method area(). The shape is a generic class and we cannot give the area of the square, triangle or circle in the generic class. polymorphism allows us to do this. polymorphism occurs when classes are interconnected with inheritance.
// generic class
public class Shape{
Public void area(){
System.out.print(“area of shape”)
}
}
// sub-class
public class Square extends Shape{
@override
// method override
Public void area(){
System.out.print(“Area of Square”)
}
}
If we declare the method in the child class, the same as we declare in the parent class, it is called method overriding and ‘@override’ is used to override the method.
If we declare the methods within the same class with different parameters then it is called method overloading.
Interface — Java
An interface is just like a class, but it contains static constants and by default, methods are declared as abstract methods.
- “implements” keyword is used to implement the interface
- java uses multiple interfaces to implement multiple inheritance.
- interface specify what will class do, no how.
- If the class implements the interface and not provide definitions of all the methods is declared in the interface then it must be declared abstract.
interface interface_name{
//constant fields
// methods
}
Example
interface Player
{
final int id=10;
int move();
}
// class implements the Player interface
class Test implements Player{
//definition of method move();
}
Conclusion
In this article, we start from the very basic to object-oriented concepts in java like inheritance, polymorphism, abstraction, encapsulation and we try to relate each concept with real-life to give you the strong basic concept of each topic. I hope so you enjoyed this article.
your feedback and questions are more than welcome!
Reference
PAUL DIETEL- HARVEY DIETEL, Java How to Program Early Objects, 10th ed., Pearson, 2014