Docsity

Java at a Glance: Core Java, OOP, and Exception Handling for Beginners, Exams of Education & Teaching

Toggle sidebar
bg1
JAVA AT A
GLANCE
What is Java?
Java is a class-based, object-oriented programming (OOP) language for building
web and desktop applications. It is the most popular programming language and
the language of choice for Android programming.
What is a Class in Java?
A class is a blueprint or a set of instructions to build a specific type of object. It is
a basic concept of object-oriented programming, which revolves around real-life
entities. A class in Java determines how an object will behave and what the object
will contain.
A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created. It is a logical entity. It cannot be
physical.
What is an Object in Java?
An object is an instance of a class.
OR
An entity that has state and behavior is known as an object. An object has three
characteristics:
State
Behavior
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java at a Glance: Core Java, OOP, and Exception Handling for Beginners and more Exams Education & Teaching in PDF only on Docsity!

JAVA AT A

GLANCE

What is Java?

Java is a class-based, object-oriented programming (OOP) language for building web and desktop applications. It is the most popular programming language and the language of choice for Android programming.

What is a Class in Java?

A class is a blueprint or a set of instructions to build a specific type of object. It is a basic concept of object-oriented programming, which revolves around real-life entities. A class in Java determines how an object will behave and what the object will contain. A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It cannot be physical.

What is an Object in Java?

An object is an instance of a class. OR An entity that has state and behavior is known as an object. An object has three characteristics: State Behavior

Identity · Differences between Objects and Classes: There are many differences between objects and classes. A list of differences between objects and classes is given below: No. Object Class

  1. An object is an instance of a class. A class is a blueprint or template from which objects are created.
  2. An object is a real-world entity such as a pen, laptop, mobile phone, bed, keyboard, mouse, chair, etc. A class is a group of similar objects.
  3. An object is a physical entity. A class is a logical entity.
  4. An object is usually created using the new keyword ; for example: Student s1=new Student (); A class is declared using the class keyword ; for example: class Student {}
  5. An object is created many times as required. A class is declared once.
  6. An object allocates memory when it is created. A class does not allocate memory when it is created.
  7. There are many ways to create objects in Java, such as the new keyword, the newInstance() method, the clone() method, a factory method, and deserialization. There is only one way to define a class in Java, using the class keyword.

public My Class {public void write Text(String text) { System. out. print(text); //prints the text parameter to System.

 How do you declare a parameterized method in Java?

Declaring parameters

The parameters are placed in a parameter list inside the parentheses that follow the method name. For each parameter used by the method, you list the parameter type followed by the parameter name. If you need more than one parameter, you separate the parameters with commas. public void getGrade ( int marks) { Final.getGrade(59);

Loop

A loop is a structure, series, or process whose end is connected to its beginning.

What are the 4 types of loops in Java?

(A) for loop (initialize; conditions; inc/dec) { //code (B) Enhanced for loop (C) while loop while (Condition) {

(D) do-while loop Do {

} while(Condition);

· What is a Switch Case, and How Do You Use It in Java?

The switch statement or switch case in Java is a multiway branch statement. Based on the value of the expression given, different parts of code can be executed quickly. The given expression can be of a primitive data type such as int, short, byte, or char.

 Use of Switch Case

The switch case in Java is used to select one of many code blocks for execution.

Syntax is the arrangement of words and phrases to create well-formed sentences in a language.

· What is a Constructor in Java?

A constructor is a special method that is used to initialize a newly created object and is called just after memory is allocated for the object. It can be used to initialize the objects to desired values or default values at the time of object creation. It is not mandatory for the coder to write a constructor for a class.

 Rules for creating a Java Constructor

  1. It has the same name as the class.
  2. It should not return a value, not even void.

 What are the 3 types of constructors?

 Types of Constructors

There are three types of constructors: a. Default constructor b. No-arg constructor c. Parameterized constructor OOP CONCEPTS IN JAVA OBJECT-ORIENTED PROGRAMMING (OOP)

Java OOP concepts

=> Inheritance => Abstraction => Encapsulation => Polymorphism Inheritance in Java OOP: Learn Different Types with Examples

 What is Inheritance?

Inheritance is a mechanism in which one class acquires the properties of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates reusability and is an important concept of OOP.

 Types of Inheritance

Here are the different types of inheritance in Java: Syntax: Class A(child) extends Class B(parent)

(B) Multiple Inheritance: Multiple inheritance is a type of inheritance in Java in which one class extends more than one class. Java does not support multiple inheritance. Java Multiple Inheritance As shown in the above diagram, Class C extends both Class A and Class B.

(C) Multilevel Inheritance: In multilevel inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class. Multilevel Inheritance As shown in the diagram, Class C is a subclass of Class B, and Class B is a subclass of Class A. (D) Hierarchical Inheritance:

Hybrid Inheritance in Java As shown in the above example, all the public and protected members of Class A are inherited into Class D, first via Class B and then via Class C. Note: Java doesn’t support hybrid/multiple inheritance. Abstraction in Java OOP | Abstract Class & Method with an Example

 What is Abstraction in Java?

Abstraction in Java shows only the essential attributes and hides unnecessary details of the object from the user. In Java, abstraction is accomplished using

abstract classes, abstract methods, and interfaces. Abstraction helps in reducing programming complexity and effort. An abstract method in Java is a method that has just the method definition but does not contain an implementation. A method without a body is known as an abstract method. It must be declared in an abstract class. The abstract method will never be final because the abstract class must implement all the abstract methods. What is an Interface in Java? How to Implement an Interface with an Example Encapsulation in Java OOP with an Example

 What is Encapsulation in Java?

Encapsulation in Java is a mechanism to wrap up variables (data) and methods (code) together as a single unit. It is the process of hiding information details and protecting the data and behavior of the object. It is one of the four important OOP concepts. The encapsulated class is easy to test, so it is also better for unit testing.

· Getters and Setters in Java

Getters and setters in Java are two conventional methods used to retrieve and update values of a variable. They are mainly used to create, modify, delete, and view the variable values. The setter method is used for updating values, and the

Try-Catch in Java: Exception Handling Example

· What is an Exception in Java?

An exception in Java is an event that interrupts the execution of program instructions and disturbs the normal flow of program execution. It is an object that wraps information about an error event that occurred within a method, and it is passed to the runtime system. In Java, exceptions are mainly used for indicating different types of error conditions.

· What is a Static Variable in Java?

A static variable in Java is a variable that belongs to the class and is initialized only once at the start of the execution. It is a variable which belongs to the class and not to an object (instance). Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.  A single copy is shared by all instances of the class.  A static variable can be accessed directly by the class name and doesn’t need an object.