Classes in Java







Introduction

 Java is an Object-Oriented Language. As a language that has the ObjectOriented feature, Java supports the following fundamental concepts −

●    Polymorphism 
●    Inheritance 
●    Encapsulation 
●    Abstraction 
●    Classes 
●    Objects 
●    Instance 
●    Method 
●    Message Parsing

Flow of Control

● Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code.

- Selection (Decision-making) statements (if-then, if-thenelse, switch) 
– Looping statements (for, while, do-while) 
– Branching statements (break, continue, return) 


abc



Selection Statements

● Selection statements allow you to control the flow of program execution on the basis of the outcome of an expression or state of a variable known during runtime. 

● Selection statements can be divided into the following categories
●    The if and if-else statements 
●    The if-else statements 
●    The if-else-if statements 
●    The switch statements  

Iteration Statements

● Repeating the same code fragment several times until a specified condition is satisfied is called iteration. 
● Iteration statements execute the same set of instructions until a termination condition is met. 

● Java provides the following loop for iteration statements: 
●    The while loop 
●    The for loop 
●    The do-while loop 
●    The for each loop 


Jump Statements

● Jump statements are used to unconditionally transfer the program control to another part of the program. 

● Java provides the following jump statements: 
●    break statement 
●    continue statement 
●    return statement 

Classes & Objects

 Objects-and-Classes-in-Java-01

Object - Objects have states and behaviors. 

Example: A dog has states – color, name, breed as well as behaviors – wagging the tail, barking, eating. 

● An object is an instance of a class. 

 ● Class - A class can be defined as a template / blueprint that describes the behavior / state that the object of its type support. 

● A class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind, 

● We can think of class as a user-defined data types and an object as a variable of that data type that can contain data and methods. 

 

Classes & Objects Example:-
public class Dog 

{     

String breed;    

int age;     

String color;   

void barking()

     { }    

 void hungry()     

{ }     

void sleeping()     

{ } 

Variables:-

● A class can contain any of the following variable types. 

Local variables: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed. 

Instance variables: Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class. 

 

 

Class variables: Class variables are variables declared within a class, outside any method, with the static keyword.


● A class can have any number of methods to access the value of various kinds of methods. 

● In the above example, barking(), hungry() and sleeping() are methods. 

 

● A class provides the blueprints for objects. So basically, an object is created from a class. In Java, the new keyword is used to create new objects. 

● There are three steps when creating an object from a class: 

Declaration: A variable declaration with a variable name with an object type. 

Instantiation: The 'new' keyword is used to create the object. 

Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object. 

 

NeXt Artical Related Methods AND access modifier in Java 

https://www.thebabatalks.com/2019/07/scanner-class-in-java.html

 

 

Image result for method in java

 

Don't Forget to Share your Opinion About This post in Comment Section, Your One Comment Will Not only Make Our day But will Make our Year. And Do mention Of you have any ideas for our Blog:)

 

Post a Comment

11 Comments

  1. This is really helpful ,thanks to the developers

    ReplyDelete