Essential C++ Concepts: Constructors, Inheritance, and Loops
Constructors in C++
Definition: A constructor is a special member function of a class that is automatically called when an object of the class is created. It is mainly used to initialize the data members of the class. The constructor has the same name as the class and it does not have any return type.
Characteristics
- Constructor name is the same as the class name.
- It has no return type (not even void).
- It is automatically executed when the object is created.
- It initializes the data members of the class.
Java Polymorphism, Overloading, Lambdas & Exceptions
Constructor and Method Overloading
class Calculator {
// Constructor overloading: same name, different parameters
Calculator() { System.out.println("Default Calculator created"); }
Calculator(String mode) { System.out.println(mode + " Calculator created"); }
// Method overloading: same name, different parameters
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
public class OverloadingDemo {
public static void main(String[] args) Read More
Java OOP Code Samples: Shapes, Resizable, Inner Classes
Java OOP Code Samples: Shapes, Resizable, Inner Classes
Summary: This page presents several Java object-oriented examples in one place. Examples include abstract classes for shapes, a resizable Rectangle via an interface, nested inner classes, a custom exception, and a small package usage example. The original code is preserved as a preformatted block below.
Complete Java Examples (original content preserved)
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter( Read More
Core Java Programming Examples Collection
Core Java Programming Examples
This document contains several fundamental Java code examples demonstrating various programming concepts.
1. Reverse an Integer
The ReverseNumber class demonstrates how to reverse an integer using a while loop.
public class ReverseNumber {
public static void main(String[] args) {
int number = 1234;
int reverse = 0;
int original = number;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + Essential Java Algorithms and Programming Fundamentals
Bubble Sort Algorithm in Java
Bubble Sort is a fundamental sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It is known for its simplicity, though it is inefficient for large lists.
import java.util.Scanner;
class program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
int n = sc.nextInt();
int arr[ Read More
Essential Java Concepts: OOP, JVM, and Exception Handling
Java Program: Convert String to Uppercase
This simple Java program demonstrates how to use the built-in toUpperCase() method of the String class.
public class UpperCase
{
public static void main(String[] args)
{
String str = "hello world";
System.out.println(str.toUpperCase());
}
}Understanding JVM and JDK
JVM (Java Virtual Machine)
The JVM is the runtime engine that executes the Java bytecode. It is an abstract machine that enables Java to be platform-independent, adhering
Read More