Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

1,989 views

Published on

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

Published in: Technology
  • Login to see the comments

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

  1. 1. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING What is Hadoop?
  2. 2. ` 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
  3. 3. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING What is a Class?
  4. 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
  5. 5. ` 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
  6. 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
  7. 7. ` 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
  8. 8. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Constructors
  9. 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
  10. 10. ` 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 :
  11. 11. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Access Modifiers
  12. 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.
  13. 13. ` 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
  14. 14. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Structure of Classes
  15. 15. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Structure of Classes Constructor Member variables Method Name of the Class
  16. 16. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Overview of Objects
  17. 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 )
  18. 18. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Working with Objects
  19. 19. ` 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?
  20. 20. ` 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
  21. 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?
  22. 22. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Working with Multiple Classes
  23. 23. ` 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 :
  24. 24. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING OOPS Concepts
  25. 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
  26. 26. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Types of Inheritance Inheritance Single HierarchicalMultilevel Hybrid
  27. 27. ` 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
  28. 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. 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. 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
  31. 31. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Encapsulation
  32. 32. ` 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
  33. 33. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Encapsulation Output : Getter and setter methods ABC
  34. 34. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Abstraction
  35. 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. 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
  37. 37. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Interface
  38. 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. 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?
  40. 40. ` https://www.edureka.co/java-j2ee-soa-trainingEDUREKA JAVA CERTIFICATION TRAINING Summary
  41. 41. ` 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

×