`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
What is Hadoop?
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Agenda For Today
➢ What is a Class?
➢ Constructors
➢ Structure of a Class
➢ Overview of objects
➢ Working with objects
➢ Using Multiple classes
➢ OOPS concepts
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
What is a Class?
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
What is a Class?
 A class in java is a blueprint which includes all the data.
 It describes the state and behavior of a specific object.
Classes
Example :
Public class car {
Color
Model
Price
speedUp();
gearChange();
}
Syntax :
Variables
Class name
Car
Methods
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Member Variables
Member variables are used to store a data value
Types
Class variableInstance variableLocal variable
Member
variables
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Types of Variables
Local variable Instance variable Class variable
Local variables are declared
within the method of a class
Instance variable are declared in a
class but outside a method,
constructor or any block
Class / static variable has only one
copy that is shared by all the
different objects of a class
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Methods
 Describes the behavior of an object.
 Collection of statements that are grouped together to perform an operation.
Methods
Example : Name of the method
Functionality
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Constructors
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Constructors
 Block of code used to initialize an object
 Must have same name of the class
 No return type
 Automatically called when an object is created
Constructors
Type of Constructors
Default Constructor
Parameterized
Constructor
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Types of Constructors
➢ No argument
➢ Compiler provides default value, automatically created
➢ Accept arguments
➢ Provides different value to objects
Default Parameterized
Example : Example :
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Access Modifiers
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Access Modifiers
Public Private
Protected Default
Access Modifiers
Class, methods, variables and
constructors can be accessed from
any other class.
No modifier required. Access class,
variables, method in same package
but not from outside.
Methods, variables and constructors
are declared protected in a
superclass can be accessed only by
the subclasses.
Methods, variables and constructors
can only be accessed within the
declared class.
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Access Modifiers
Access Modifiers Same Class Same Package Sub Class Other Packages
Public Y Y Y Y
Private Y N N N
Protected Y Y Y N
Default Y Y N N
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Structure of Classes
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Structure of Classes
Constructor
Member variables
Method
Name of the Class
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Overview of Objects
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Overview of Objects
 It is an entity that has state and behavior
 Instance of a class which can access data
Objects
➢ States of a Car : Color - Orange
Model - 1
Price - 50000
➢ Behavior of Car : Speed up
Change gear
Class - Car
(Blueprint )
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Working with Objects
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
InitializationInstantiationDeclaration
Working with Objects
Variable name
with a data type
“New” keyword
creates an object
Call to a constructor,
initializes new object
How can you create an object?
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Working with Objects
Syntax :
 Objects to a class is created by using the keyword “new”.
 New allocates memory for the object.
Student student1 = new Student();
Object Name
Class Name Keyword
Constructor
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Working with Objects
Store data into object using a
constructor
Constructor
Stores data into objects by
invoking a method.
Method
Stores data into object through
reference variable
Reference
variable
Initialize an object?
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Working with Multiple Classes
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Working with Multiple Classes
Constructor of Class A
Method of class A
Constructor of class B
Method of class B
Output :
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
OOPS Concepts
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Inheritance
 One class acquire properties (methods & fields) of another
 Class which inherits the properties – Child class/ derived class/ sub class
 Class whose properties are inherited – Parent class/ base class
Inheritance
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Types of Inheritance
Inheritance
Single HierarchicalMultilevel Hybrid
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Class B inherits the properties of A
Inheritance
Class A {
----
}
Public class B extends A {
----
----
}
Syntax:
Single
Hierarchical
Multilevel
Hybrid
Class A
Class B
Parent Class
Child class of A
Single
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Inheritance
Class A
Class B
Class C
Class A {
----
}
public class B extends A {
---
}
Public class C extends B {
---
}
Syntax:
Single
Hierarchical
Multilevel
Hybrid
Class B inherits the properties of A and Class B inherits the
properties of B
Parent class of B
Parent class of C
Child class of A
Child class of B
Multilevel
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Inheritance
Class A
Class B Class C
Class A {
----
}
public class B extends A {
----
}
Public class C extends A {
----
}
Syntax:
Single
Hierarchical
Multilevel
Hybrid
One class is inherited by many subclasses
Parent Class
Child Classes
Hierarchical
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Parent class for class D,
Child class of class A
Inheritance
Class A
Class B Class C
Class D
Single
Hierarchical
Multilevel
Hybrid
 Single + Multiple inheritance
 It can only be achieved through Interface.
Parent class for class B and class C
Hybrid
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Encapsulation
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Encapsulation
Encapsulation
 Binding the data and code together as a single unit.
 Securing data by hiding the implementation details to user.
Methods Variable
Class
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Encapsulation
Output :
Getter and setter methods
ABC
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Abstraction
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Abstraction
Abstraction
 Hides the implementation details and only provides the functionality to the user
 You can achieve abstraction using Abstract classes and Interfaces
Without
Abstraction
With
Abstraction
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Abstract Class
How can you use an abstract class?
Inherit it from another class and provide implementations to the abstract methods in it.
If a class has one abstract method, then the class must be declared abstract.
1
2
abstract class <class-name> { }
 Class contains the abstract keyword
 If a class is declared abstract, it cannot be instantiated
Syntax :
Abstract class
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Interface
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Interfaces
interface Car {
void changeGear (int newValue);
void speedup (int increment);
void applyBrakes (int decrement);
}
Interfaces
 Interface in java is a blueprint of a class.
 Each method in an interface are implicitly public and abstract.
 It does not contain any constructors.
Example :
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
class Audi implements Car {
int speed = 0;
int gear = 1;
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println(" speed:" +
speed + " gear:" + gear);
}
}
Interfaces
To implement an interface?
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Summary
`
https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING
Session In A Minute
Java Classes Constructors Access Modifiers
Structure of a Class Overview of Objects OOPS Concepts
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java Training | Edureka

Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java Training | Edureka