java cheat
java cheat
Q2.(a) what is the difference between object oriented programing and Q4. explain different types of inheritance in java with examples
Object-Oriented Programming (OOP) is based on three fundamental procedural progrimming Inheritance in Java allows a class (child class or subclass) to acquire
principles: Encapsulation, Inheritance, and Polymorphism. These Object-Oriented Programming (OOP) and Procedural Programming are two properties and behaviors of another class (parent class or superclass).
principles define the core structure and behavior of OOP and enable the fundamental programming paradigms. They differ in their approach to Java supports multiple types of inheritance to promote code reuse and
creation of modular, reusable, and maintainable code. Here's an structuring and organizing code. Here's a detailed comparison: modularity, but multiple inheritance with classes is not allowed directly
explanation of each: 1. Paradigm Focus to avoid ambiguity.
1. Encapsulation OOP: Here are the different types of inheritance in Java:
Definition: Encapsulation is the bundling of data (attributes) and - Focuses on objects, which are instances of classes. Single Inheritance
methods (functions) that operate on the data into a single unit, typically - Emphasizes real-world modeling through data and behaviors (methods). Definition: A subclass inherits from a single superclass.
a class. It also involves restricting direct access to some of the object's Procedural Programming: Purpose: To reuse the properties and methods of a single class.
components to protect the integrity of the data. - Focuses on a sequence of instructions or procedures (functions) to operate Example:
Purpose: on data. class Animal {
-To ensure that an object's internal state cannot be changed directly, - Emphasizes step-by-step execution of tasks. void eat() {
promoting data security. 2. Structure System.out.println("This animal eats food.");}}
-To define a clear interface for interacting with an object. OOP: class Dog extends Animal {
2. Inheritance - Organized around classes and objects. void bark() {
Definition: Inheritance allows a new class (child class) to acquire the - Data and methods are encapsulated in objects. System.out.println("The dog barks.");}}
properties and behaviors of an existing class (parent class). This enables Procedural Programming: Multilevel Inheritance
code reuse and the creation of hierarchical relationships. - Organized into functions and procedures. Definition: A class inherits from another class, and another class inherits
Purpose: - Data is separate from functions. from it, forming a chain.
-To avoid code duplication by reusing existing code. 3. Data Handling Purpose: To extend functionality across multiple levels.
-To establish a natural hierarchy between classes. OOP: Example:
3. Polymorphism - Data is hidden (encapsulation) and accessed through methods. class Animal {
Definition: Polymorphism allows objects of different classes to be - Focuses on protecting data and defining strict interfaces. void eat() {
treated as objects of a common parent class. It also allows the same Procedural Programming: System.out.println("This animal eats food.");}}
method name to have different implementations based on the object. - Data is often stored in global variables or passed between functions. class Mammal extends Animal {
Purpose: - No inherent mechanism to protect data. void walk() {
-To enable flexibility and extendability in code. Q2. (b) why java is platform indipendent ? explain System.out.println("Mammals walk.");}}
-To allow different classes to provide their specific implementations of - When you write Java code, it is saved in a file with the .java extension
methods. -Bytecode is not machine-specific but rather a standardized, portable
format.
-The JVM acts as a bridge, adapting the bytecode to the specific machine
Q2.(a) what is the difference between object oriented programing and
and operating system.
Notes on Class and Object procedural progrimming
-Bytecode: A universal, intermediate representation of the Java code.
Class Object-Oriented Programming (OOP) and Procedural Programming are two
-Portability: Applications can run on multiple platforms without
Definition: A blueprint or template for creating objects. It defines the fundamental programming paradigms. They differ in their approach to
modification.
attributes (data) and methods (behavior) that the objects created from structuring and organizing code. Here's a detailed comparison:
it will have. 1. Paradigm Focus
Key Features: OOP:
Acts as a logical container for related data and methods. - Focuses on objects, which are instances of classes.
Provides a structure for creating multiple objects with similar properties - Emphasizes real-world modeling through data and behaviors (methods).
Notes on Class and Object Procedural Programming:
and behavior.
Class - Focuses on a sequence of instructions or procedures (functions) to operate
Classes support encapsulation, abstraction, and inheritance.
Definition: A blueprint or template for creating objects. It defines the on data.
Object
attributes (data) and methods (behavior) that the objects created from - Emphasizes step-by-step execution of tasks.
Definition: An instance of a class that represents a specific entity with
it will have. 2. Structure
defined properties and behaviors.
Key Features: OOP:
Key Features:
Acts as a logical container for related data and methods. - Organized around classes and objects.
An object is a real-world entity modeled in the program.
Provides a structure for creating multiple objects with similar properties - Data and methods are encapsulated in objects.
It has a state (attributes) and behavior (methods).
and behavior. Procedural Programming:
Objects interact with each other via methods.
Classes support encapsulation, abstraction, and inheritance. - Organized into functions and procedures.
Method Overloading
Object - Data is separate from functions.
Definition: A feature in Java where multiple methods in the same class
Definition: An instance of a class that represents a specific entity with 3. Data Handling
have the same name but different parameter lists.
defined properties and behaviors. OOP:
Key Features:
Key Features: - Data is hidden (encapsulation) and accessed through methods.
-Occurs within the same class.
An object is a real-world entity modeled in the program. - Focuses on protecting data and defining strict interfaces.
-Differentiated by the number, type, or order of parameters.
It has a state (attributes) and behavior (methods). Procedural Programming:
-Enables methods to perform similar functions with different inputs.
Objects interact with each other via methods. - Data is often stored in global variables or passed between functions.
Method Overriding
Method Overloading - No inherent mechanism to protect data.
Definition: A feature in Java where a subclass provides a specific
Definition: A feature in Java where multiple methods in the same class Q2. (b) why java is platform indipendent ? explain
implementation of a method already defined in its parent class.
have the same name but different parameter lists. - When you write Java code, it is saved in a file with the .java extension
Key Features:
Key Features: -Bytecode is not machine-specific but rather a standardized, portable
-Occurs between two classes (parent and child).
-Occurs within the same class. format.
-The method in the subclass must have the same name, return type,
-Differentiated by the number, type, or order of parameters. -The JVM acts as a bridge, adapting the bytecode to the specific machine
and parameter list as the parent class method.
-Enables methods to perform similar functions with different inputs. and operating system.
-Used to achieve runtime polymorphism.
Method Overriding -Bytecode: A universal, intermediate representation of the Java code.
Static
Definition: A feature in Java where a subclass provides a specific -Portability: Applications can run on multiple platforms without
Definition: A keyword used to declare variables, methods, blocks, or
implementation of a method already defined in its parent class. modification.
nested classes that belong to the class rather than to any specific
Key Features:
instance of the class.
-Occurs between two classes (parent and child).
- Shared across all instances of a class.
- Memory is allocated only once when the class is loaded. -The method in the subclass must have the same name, return type,
- Often used for constants or counters. and parameter list as the parent class method.
Final -Used to achieve runtime polymorphism.
Definition: A keyword used to declare constants, prevent inheritance, Static
and ensure immutability. Definition: A keyword used to declare variables, methods, blocks, or
- Value cannot be changed once initialized. nested classes that belong to the class rather than to any specific
-Must be initialized when declared or in the constructor. instance of the class. Q1. explain three fundamental principle of object oriented pogriming
-Cannot be overridden by subclasses. - Shared across all instances of a class. Object-Oriented Programming (OOP) is based on three fundamental
- Memory is allocated only once when the class is loaded. principles: Encapsulation, Inheritance, and Polymorphism. These
- Often used for constants or counters. principles define the core structure and behavior of OOP and enable the
Final creation of modular, reusable, and maintainable code. Here's an
Definition: A keyword used to declare constants, prevent inheritance, explanation of each:
and ensure immutability. 1. Encapsulation
Q4. explain different types of inheritance in java with examples - Value cannot be changed once initialized. Definition: Encapsulation is the bundling of data (attributes) and
Inheritance in Java allows a class (child class or subclass) to acquire -Must be initialized when declared or in the constructor. methods (functions) that operate on the data into a single unit, typically
properties and behaviors of another class (parent class or superclass). -Cannot be overridden by subclasses. a class. It also involves restricting direct access to some of the object's
Java supports multiple types of inheritance to promote code reuse and
components to protect the integrity of the data.
modularity, but multiple inheritance with classes is not allowed directly
Purpose:
to avoid ambiguity.
-To ensure that an object's internal state cannot be changed directly,
Here are the different types of inheritance in Java:
promoting data security.
Single Inheritance
-To define a clear interface for interacting with an object.
Definition: A subclass inherits from a single superclass.
2. Inheritance
Purpose: To reuse the properties and methods of a single class.
Definition: Inheritance allows a new class (child class) to acquire the
Example:
properties and behaviors of an existing class (parent class). This enables
class Animal {
code reuse and the creation of hierarchical relationships.
void eat() {
Purpose:
System.out.println("This animal eats food.");}}
-To avoid code duplication by reusing existing code.
class Dog extends Animal {
-To establish a natural hierarchy between classes.
void bark() {
3. Polymorphism
System.out.println("The dog barks.");}}
Definition: Polymorphism allows objects of different classes to be
Multilevel Inheritance
treated as objects of a common parent class. It also allows the same
Definition: A class inherits from another class, and another class inherits
method name to have different implementations based on the object.
from it, forming a chain.
Purpose:
Purpose: To extend functionality across multiple levels.
-To enable flexibility and extendability in code.
Example:
-To allow different classes to provide their specific implementations of
class Animal {
methods.
void eat() {
System.out.println("This animal eats food.");}}
class Mammal extends Animal {
void walk() {
System.out.println("Mammals walk.");}}