This Edureka 'Java Classes' tutorial will take you through in-depth details of Java classes and different key-components in Java. It is ideal for both beginners and professionals who want to learn or brush up the basics of Java. Below are the topics covered in this tutorial:
1. Classes in Java
2. Constructors
3. Structure of a Class
4. Overview of Objects
5. Working with Objects
6. Multiple classes
7. OOPS concepts: Inheritance, Encapsulation, Abstraction, Interface
4. `
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
6. `
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
9. `
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
12. `
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.
17. `
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 )
21. `
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?
25. `
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
28. `
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
29. `
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
30. `
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
35. `
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
36. `
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
38. `
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 :
39. `
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?