Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Java Definitions

The document provides an overview of key concepts in Java programming, including classes, objects, encapsulation, inheritance, polymorphism, and various types of operators. It explains fundamental principles such as method overloading, method overriding, and the importance of encapsulation and abstraction. Additionally, it covers multithreading, string manipulation, and different types of inheritance, alongside the significance of operators in efficient coding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Definitions

The document provides an overview of key concepts in Java programming, including classes, objects, encapsulation, inheritance, polymorphism, and various types of operators. It explains fundamental principles such as method overloading, method overriding, and the importance of encapsulation and abstraction. Additionally, it covers multithreading, string manipulation, and different types of inheritance, alongside the significance of operators in efficient coding.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Class: A class is a blueprint for creating objects.

It defines attributes (variables) and


behaviours (methods) that an object can have. For example, a "Car" class may
include attributes like colour and speed and methods like drive and brake.

2. Object: An object is an instance of a class. It represents a real-world entity with state


and behaviour. For example, a specific car with red colour and a speed of 60 km/h is
an object of the "Car" class.

3. Encapsulation: Encapsulation is the concept of wrapping data (variables) and


methods into a single unit (class). It restricts direct access to some components,
promoting data security. For example, private variables with public getter and setter
methods.

4. Inheritance: Inheritance allows one class to acquire properties and behaviours of


another class. It promotes code reusability. For example, a "Truck" class can inherit
attributes and methods from a "Vehicle" class.

5. Polymorphism: Polymorphism enables one interface to represent multiple forms. It


can be achieved through method overloading and overriding. For example, a "Shape"
class can have different implementations of the draw () method for circles and
squares.

6. Abstraction: Abstraction hides complex implementation details and shows only the
necessary features. It can be achieved using abstract classes or interfaces. For
example, a "Remote" interface only shows essential functions like power On and
volume Up.

7. Constructor: A constructor is a special method invoked when an object is created. It


initializes the object’s state. For example, a "Person" class constructor can set name
and age attributes.

8. Method Overloading: Method overloading allows multiple methods with the same
name but different parameter lists. It helps improve code readability. For example, a
print () method that prints integers and another that prints strings.

9. Method Overriding: Method overriding allows a subclass to provide a specific


implementation of a method already defined in its superclass. For example, a Dog
class can override the make Sound () method of the Animal class.

10. Association: Association is a relationship between two classes. It can be one-to-one,


one-to-many, or many-to-many. For example, a "Teacher" class can be associated
with multiple "Student" classes.

11. Aggregation: Aggregation is a special type of association where one class contains a
reference to another class, but both can exist independently. For example, a "Library"
class containing "Book" objects.
12. Composition: Composition is a stronger form of aggregation where the contained
object cannot exist independently. For example, a "House" class composed of
"Room" objects, where rooms cease to exist without the house.

13. Interface: An interface is a contract that defines a set of abstract methods a class
must implement. It supports multiple inheritance. For example, a "Flyable" interface
with a fly() method implemented by both Bird and Airplane classes.

14. Abstract Class: An abstract class is a class that cannot be instantiated and may have
abstract methods. It provides a base for other classes to build on. For example, a
"Shape" abstract class with an abstract draw () method.

15. Static Keyword: The static keyword makes a class member belong to the class rather
than any object. For example, a static count variable shared by all instances of a class.

16. Final Keyword: The final keyword can be used with variables, methods, or classes to
restrict modifications. For example, a final class cannot be subclassed, and a final
variable cannot change its value.

17. This Keyword: This keyword refers to the current object. It is used to resolve naming
conflicts and invoke constructors. For example, this.name refers to the instance
variable name.

18. Super Keyword: The super keyword refers to the superclass and is used to access its
methods or constructors. For example, super () calls the parent class constructor.

19. Object Class: The Object class is the root class of all Java classes. It provides basic
methods like to String (), equals (), and hash Code (). For example, every Java class
inherits these methods.

20. Packages: Packages are collections of related classes and interfaces that help
organize code and avoid naming conflicts. For example, java. util contains utility
classes like Array List and HashMap.

Types of Inheritance in Java

1. Single Inheritance
Single inheritance involves one child class inheriting from a single parent class.
It helps in code reusability and allows the child class to access the parent’s methods
and properties.
Example: A "Dog" class inherits from an "Animal" class.

2. Multilevel Inheritance
Multilevel inheritance occurs when a class is derived from another derived class.
It forms a chain where each class inherits properties from its immediate superclass.
Example: "Baby Dog" → "Dog" → "Animal," where Baby Dog inherits properties from
both Dog and Animal.
3. Hierarchical Inheritance
Hierarchical inheritance involves multiple child classes inheriting from a single parent
class.
It models a tree-like structure where several classes share common properties.
Example: "Car" and "Bike" classes inherit from the "Vehicle" class.

4. Multiple Inheritance (Through Interfaces)


Java doesn’t support multiple inheritance with classes but achieves it through
interfaces.
A class can implement multiple interfaces to inherit behaviour from multiple sources.
Example: A "Printer Scanner" class implements both "Printer" and "Scanner"
interfaces.

5. Hybrid Inheritance (Through Interfaces)


Hybrid inheritance is a mix of two or more types of inheritance, achieved using
interfaces.
It combines different inheritance models to meet complex design needs.
Example: A "Smart Device" implements both "Computer" and "Phone" interfaces.

Thread in Java

A thread is a lightweight, independent path of execution within a program.


It allows multiple tasks to run concurrently, improving application performance.
Example: A music player can play songs while downloading files in the background.

Multithreading in Java

Multithreading is the ability to run multiple threads concurrently within a program.


It allows tasks to execute independently, improving performance and responsiveness.
Example: A web browser can load multiple tabs at the same time using multithreading.

String in Java

A String is an immutable sequence of characters, meaning its value cannot be changed


after creation.
It is stored in the String pool, and modifications create new objects in memory.
Example: String name = "Java"; — changing name creates a new string object.

StringBuilder in Java

StringBuilder is a mutable class that allows modifying character sequences without


creating new objects.
It is faster and more memory-efficient for frequent string manipulations.
Example: StringBuilder sb = new StringBuilder("Java"); sapped (" Rocks!"); — modifies
the same object.
Types of Polymorphism in Java

1. Compile-Time Polymorphism (Method Overloading)


Method overloading allows multiple methods in a class with the same name but
different parameters.
The method called is determined at compile time based on argument types and
counts.
Example: A Calculator class with add (int, int) and add (double, double) methods.

2. Run-Time Polymorphism (Method Overriding)


Method overriding occurs when a subclass provides a specific implementation of a
parent class method.
The method to execute is determined at runtime based on the object type.
Example: A Dog class overrides the sound () method of an Animal class to bark.

Operators in Java:

In Java, operators are special symbols or keywords used to perform operations on variables
and values. They help manipulate data, make comparisons, and control program flow.

Operators are essential building blocks of Java programs, allowing you to solve problems
efficiently!

Types of Operators in Java:

1. Arithmetic Operators
These operators perform basic mathematical calculations like addition, subtraction,
multiplication, and division.

 + (Addition): Adds two values.

 - (Subtraction): Subtracts the second value from the first.

 * (Multiplication): Multiplies two values.

 / (Division): Divides the first value by the second (returns the quotient).

 % (Modulus): Returns the remainder after division.

Example:
If a = 10 and b = 3:

 a + b results in 13

 a % b results in 1 (remainder)

2. Relational (Comparison) Operators


These operators compare two values and return a boolean result (true or false).

 == (Equal to): Returns true if values are equal.


 != (Not equal to): Returns true if values are not equal.

 > (Greater than): Returns true if the first value is greater than the second.

 < (Less than): Returns true if the first value is less than the second.

 >= (Greater than or equal to): Returns true if the first value is greater than or equal
to the second.

 <= (Less than or equal to): Returns true if the first value is less than or equal to the
second.

Example:
If x = 5 and y = 10:

 x < y results in true

 x == y results in false

3. Logical Operators
These operators are used to combine multiple boolean expressions or conditions.

 && (Logical AND): Returns true if both conditions are true.

 || (Logical OR): Returns true if at least one condition is true.

 ! (Logical NOT): Reverses the result (true becomes false, and vice versa).

Example:
If a = 5 and b = 10:

 (a > 3 && b < 15) results in true

 !(a == b) results in true (because a is not equal to b)

4. Assignment Operators
These operators assign values to variables.

 = (Simple assignment): Assigns the value on the right to the variable on the left.

 += (Add and assign): Adds the right value to the left variable and assigns the result.

 -= (Subtract and assign): Subtracts the right value from the left variable and assigns
the result.

 *= (Multiply and assign): Multiplies and assigns the result.

 /= (Divide and assign): Divides and assigns the quotient.


 %= (Modulus and assign): Divides and assigns the remainder.

Example:
If x = 5:

 x += 3 → x becomes 8

 x *= 2 → x becomes 16

5. Unary Operators
These operators work with a single operand.

 + (Unary plus): Indicates a positive value (optional).

 - (Unary minus): Negates a value.

 ++ (Increment): Increases a value by 1.

o Pre-increment (++x) → Increments before using the value.

o Post-increment (x++) → Increments after using the value.

 -- (Decrement): Decreases a value by 1.

o Pre-decrement (--x) → Decrements before using the value.

o Post-decrement (x--) → Decrements after using the value.

Example:
If x = 5:

 ++x makes x → 6

 x-- makes x → 5 (after using 6)

6. Bitwise Operators
Operate on bits of integer values.

 & (Bitwise AND)

 | (Bitwise OR)

 ^ (Bitwise XOR)

 ~ (Bitwise NOT)

 << (Left shift)

 >> (Right shift)


Example:
5 & 3 → 1 (because 0101 & 0011 = 0001 in binary)

7. Ternary Operator
A shorthand for if-else statements.

 Syntax: condition? value_if_true : value_if_false

Example:

int a = 10, b = 20;

int max = (a > b) ? a : b; // max = 20

Why Are Operators Important?

 Efficient Computations: Perform quick calculations.

 Conditional Logic: Control program flow with comparisons and logical checks.

 Simplified Code: Make code shorter and more readable.

You might also like