500 likes | 1.62k Views
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: <br><br>1. Classes in Java <br>2. Constructors <br>3. Structure of a Class <br>4. Overview of Objects <br>5. Working with Objects <br>6. Multiple classes <br>7. OOPS concepts: Inheritance, Encapsulation, Abstraction, Interface
E N D
` EDUREKA JAVA CERTIFICATION TRAINING What is Hadoop? https://www.edureka.co/java-j2ee-soa-training
Agenda For Today ➢ What is a Class? ➢ Constructors ➢ Structure of a Class ➢ Overview of objects ` ➢ Working with objects ➢ Using Multiple classes ➢ OOPS concepts EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
What is a Class? ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-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 Syntax : Example : ` Class name Public class car { Car Color Model Price speedUp(); gearChange(); } Variables Methods EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Member Variables Member variables Member variables are used to store a data value Types ` Local variable Instance variable Class variable EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Types of Variables Local variable Instance variable Class variable ` 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 Local variables are declared within the method of a class EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Methods Describes the behavior of an object. Collection of statements that are grouped together to perform an operation. Methods ` Name of the method Example : Functionality EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Constructors ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-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 Parameterized Constructor Default Constructor EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Types of Constructors Default Parameterized ➢ No argument ➢ Accept arguments ➢ Compiler provides default value, automatically created ➢ Provides different value to objects ` Example : Example : EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Access Modifiers ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Access Modifiers Public Private Class, methods, variables and constructors can be accessed from any other class. Methods, variables and constructors can only be accessed within the declared class. ` Access Modifiers Protected Default Methods, variables and constructors are declared protected in a superclass can be accessed only by the subclasses. No modifier required. Access class, variables, method in same package but not from outside. EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-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 EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Structure of Classes ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Structure of Classes Name of the Class Member variables ` Constructor Method EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Overview of Objects ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Overview of Objects It is an entity that has state and behavior Instance of a class which can access data Objects ` Class - Car (Blueprint ) ➢ States of a Car : Color - Orange Model - 1 Price - 50000 ➢ Behavior of Car : Speed up Change gear EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Working with Objects ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Working with Objects How can you create an object? ` Declaration Initialization Instantiation Call to a constructor, initializes new object Variable name with a data type “New” keyword creates an object EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Working with Objects Objects to a class is created by using the keyword “new”. New allocates memory for the object. Syntax : Class Name Keyword ` Student student1 = new Student(); Object Name Constructor EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Working with Objects Store data into object using a constructor Constructor ` Stores data into objects by invoking a method. Method Initialize an object? Stores data into object through reference variable Reference variable EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Working with Multiple Classes ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Working with Multiple Classes Output : Constructor of Class A Method of class A ` Constructor of class B Method of class B EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
OOPS Concepts ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-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 ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Types of Inheritance Inheritance ` Single Multilevel Hierarchical Hybrid EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Inheritance Single Class B inherits the properties of A Single Syntax: Multilevel ` Class A { ---- } Public class B extends A { ---- ---- } Hierarchical Parent Class Class A Hybrid Child class of A Class B EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Inheritance Class B inherits the properties of A and Class B inherits the properties of B Multilevel Single Syntax: Multilevel ` Class A { ---- } public class B extends A { --- } Public class C extends B { --- } Parent class of B Class A Hierarchical Hybrid Parent class of C Child class of A Class B Child class of B Class C EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Inheritance Hierarchical One class is inherited by many subclasses Single Syntax: Multilevel ` Class A { ---- } public class B extends A { ---- } Public class C extends A { ---- } Hierarchical Parent Class Class A Hybrid Class B Class C Child Classes EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Inheritance Single + Multiple inheritance It can only be achieved through Interface. Hybrid Single Multilevel Parent class for class B and class C Class A ` Hierarchical Parent class for class D, Child class of class A Hybrid Class B Class C Class D EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Encapsulation ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Encapsulation Binding the data and code together as a single unit. Securing data by hiding the implementation details to user. Encapsulation ` Class Methods Variable EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Encapsulation Getter and setter methods ` Output : ABC EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Abstraction ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Abstraction Hides the implementation details and only provides the functionality to the user You can achieve abstraction using Abstract classes and Interfaces Abstraction Without Abstraction With Abstraction ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Abstract Class Class contains the abstract keyword If a class is declared abstract, it cannot be instantiated Abstract class Syntax : abstract class <class-name> { } ` How can you use an abstract class? Inherit it from another class and provide implementations to the abstract methods in it. 1 2 If a class has one abstract method, then the class must be declared abstract. EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Interface ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
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. Interfaces Example : ` interface Car { void changeGear (int newValue); void speedup (int increment); void applyBrakes (int decrement); } EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Interfaces To implement an interface? 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); } } ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Summary ` EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training
Session In A Minute Constructors Java Classes Access Modifiers ` Structure of a Class Overview of Objects OOPS Concepts EDUREKA JAVA CERTIFICATION TRAINING https://www.edureka.co/java-j2ee-soa-training