0% found this document useful (0 votes)
4 views6 pages

Java Theory Notes (1)

Uploaded by

Mohd Aqdas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Java Theory Notes (1)

Uploaded by

Mohd Aqdas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Programming Theory - Test Preparation Notes

1. Method in Java

-----------------

A method in Java is a block of code that performs a specific task. It is used to define the behavior of

a class.

Syntax:

returnType methodName(parameters) {

// method body

Example:

public int add(int a, int b) {

return a + b;

Full Example in a Class:

public class Calculator {

// Method to add two numbers

public int add(int a, int b) {

return a + b;

// Main method to run the program

public static void main(String[] args) {


Calculator calc = new Calculator(); // Create an object of Calculator

int result = calc.add(5, 3); // Call the add method

System.out.println("Sum: " + result); // Print the result

Output:

Sum: 8

2. Inheritance in Java

----------------------

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class

(child or subclass)

to inherit the properties and behaviors (fields and methods) of another class (parent or superclass).

It promotes code reusability and method overriding.

Syntax:

class Parent {

// parent class members

class Child extends Parent {

// child class members

Example:

class Animal {

void sound() {
System.out.println("Animal makes a sound");

class Dog extends Animal {

void bark() {

System.out.println("Dog barks");

public class Main {

public static void main(String[] args) {

Dog d = new Dog();

d.sound(); // Inherited from Animal

d.bark(); // Defined in Dog

Output:

Animal makes a sound

Dog barks

3. Data Types in Java

---------------------

In Java, data types specify the type of data a variable can hold. Java is a strongly-typed language,

meaning every

variable must be declared with a data type.


Java data types are mainly divided into two categories:

1. Primitive Data Types (built-in types):

- byte (1 byte): small integers - byte b = 10;

- short (2 bytes): larger integers - short s = 1000;

- int (4 bytes): default for integers - int x = 500;

- long (8 bytes): very large integers - long l = 100000L;

- float (4 bytes): decimals (less precise) - float f = 5.5f;

- double (8 bytes): decimals (more precise) - double d = 10.99;

- char (2 bytes): single character - char c = 'A';

- boolean (1 bit): true/false - boolean flag = true;

2. Non-Primitive (Reference) Data Types:

These refer to objects and include:

- String

- Arrays

- Classes

- Interfaces

- Enums

Example:

String name = "Java";

int[] numbers = {1, 2, 3};

4. Constructor in Java

----------------------
A constructor in Java is a special method that is automatically called when an object is created.

It is used to initialize objects.

Key Features:

- Has the same name as the class.

- Has no return type, not even void.

- Can be default (no parameters) or parameterized (with parameters).

Example:

public class Person {

String name;

int age;

// Constructor

public Person(String n, int a) {

name = n;

age = a;

// Method to display information

void display() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

// Main method

public static void main(String[] args) {


Person p1 = new Person("Alice", 25); // Constructor is called here

p1.display();

Output:

Name: Alice

Age: 25

You might also like