Polymorphism in Java – OOP – Code Example

In previous posts we have discussed many important topics of object oriented programming like classes, objects, inheritance etc. In this post we will discuss polymorphism in Java which is another important OOP concept. What is Polymorphism in Java? The word polymorphism means 'a state of having many shapes'. In terms of object oriented programming it's the ability to process objects of various types and classes through a single uniform interface. An object, variable or function may take on multiple forms / shapes and this phenomenon is known as Polymorphism. Example of Polymorphism in JAVA: [js] public int add(int a, int b){...
forward

IS-A and HAS-A Relationships in Java – Code Example

Code reuse-ability is the fundamental objective of object oriented programming. To achieve code reuse-ability objective, we use IS-A, HAS-A relationships. IS-A relationship is another name of inheritance or we can say that IS-A relationship is achieved through inheritance. In inheritance child class can inherit attributes and methods from its parent class. For example we have a Student class which inherits from Human class then it means that it follows the IS-A relationship because a Student IS-A Human. Further it should be noted that a Student IS-A Human but a Human may not be a Student. It means that child class...
forward

Inheritance in Java OOP – Code Example 2

In previous post we learned about inheritance in Java programming by using simple code example. In this post we will consider another code example of inheritance in Java. In previous post we created a Car class (child class) which inherited some attributes and methods from Vehicle class (parent class). In this post we will create a Student class (child class) which will inherit attributes and methods from Human class (parent class). [js] //Student class code: package learningclassesaobjects; public class Student extends Human { int rollNo; public void setRollNo(int x){ rollNo = x; } public void getRollNo(){ System.out.printf("Your Roll No is:...
forward

Inheritance in Java OOP – Code Example 1

In our previous post we learned how to create classes and objects by using code example. In this post we will learn inheritance which is another important object oriented programming (OOP) concept. Inheritance in Java is like a parent child relationship. As child inherit some properties or behaviors from parents, similarly in inheritance (OOP Concept) a child class can inherit properties and methods from parent class. Following simple code example explains the inheritance concepts in Java programming. This code consists of 3 Java classes, main class (LearningInheritance.java), parent class (Vehicle.java) and child class (Car.java). Create a new project in netbeans (java...
forward

Classes and Objects in Java Programming – Code Example

In this post we will learn (java syntax) what are classes and objects in Java programming. Classes and objects are fundamental object oriented programming concepts. In previous post we discussed some important object oriented programming concepts. If you want to learn about classes and objects, class constructor method, constructor parameters, instance variables, class methods and main method then read this post. In this simple program we will learn how to create a class and class objects in Java programming. Further we will also learn how to create instance variables (attributes) and methods for class in Java programming. [js] package carclassjava;...
forward

Basic Data Types in Java Programming – Example Code

In previous post we learned how to write a very simple hello world Java program. In this simple program we will explain what are some different data types in Java. We use specific data types to define variable depending upon the nature of data we want to store in variable. [js] public class DataTypesJava { public static void main(String[] args) { //single line comment boolean isBoolean = true; // boolean data type store either true or false int isInt = 120; // int data type is used to store integer values int mult = isInt * 2; // multiplication is...
forward

Hello World Program in Java

In this post we will discuss and explain how to write a simple hello world program in java. To write, compile and execute a java program many IDE's are available but we will use netbeans  (www.netbeans.org). Following is the simple hello world program in java programming. Step 1: Open netbeans IDE, go into File and click New Project. A new box will appear. From categories select Java and from projects select Java Application and click Next. Step 2: Define project name and project location and make sure that Create Main Class checkbox is checked then click Finish. Step 3: All...
forward

What is Object Oriented Programming?

The ultimate goal of software development is to build a software quickly, correctly and economically. Object Oriented Programming design and implementation approach helps fulfill this goal more productively as compared with structured programming technique. Object oriented programs are often easier to understand, correct and modify. In object oriented programming we divide or organize our program using classes, objects and methods. We will discuss some object oriented programming concepts below. Objects and Classes: A class is a collection of similar attributes (also known as members or variables) and methods (also known as actions or functions) in object oriented programming. All objects...
forward

Introduction To Java Programming

There's no doubt that java programming language is one of the most powerful programming languages out there in the world. Java is not just a programming language but a technology platform with many interconnected capabilities. According to official website, more than 15 billion devices run java. Which means that an application developed using java can be used on more than 15 billion devices. According to official website of Java: "Java is at the heart of our digital lifestyle. It's the platform for launching careers, exploring human-to-digital interfaces, architecting the world's best applications, and unlocking innovation everywhere—from garages to global organizations"...
forward

Bubble Sort using Call by Reference (Pointers) C Program

In one of our previous posts we already discussed about Bubble Sort algorithm and how to implement it in C programming. In this post we will discuss about bubble sort using call by reference method. In other words we will implement the bubble sort algorithm using pointers i.e call by reference. [js] #include <stdio.h> #define SIZE 10 void bubbleSort(int * const array, const int size); void swap(int *element1Ptr, int *element2Ptr); int main(void){ int arr[SIZE] = {2, 3, 9, 4, 1, 5, 7, 12, 18, 15}; printf("Original array valuesnn"); for(int i=0; i<SIZE; i++){ printf("%dn", arr[i]); } bubbleSort(arr, SIZE); printf("nnSorted array valuesnn");...
forward