Java Classes and Objects: Definition, Syntax, and Examples

slide1 n.w
1 / 12
Embed
Share

Learn how to define new data types in Java using classes and objects. Understand the syntax for creating classes, initializing objects, and defining methods within a class. Explore examples demonstrating the concept of classes, objects, constructors, and methods in Java programming.

  • Java Programming
  • Classes
  • Objects
  • Syntax
  • Examples

Uploaded on | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Classes and Objects A class in Java defines a new data type that can include fields (variables) and methods to define the behavior of the objects created from the class. Syntax: class ClassName { // Fields dataType fieldName; // Constructor ClassName(parameters) { // Initialize fields }

  2. // Methods returnType methodName(parameters) { // Method body } } ClassName: The name of the class. fieldName: Variables that hold the state of the class. Constructor: A special method used to initialize objects. methodName: Functions defined within the class to perform actions.

  3. Objects An object is an instance of a class. It is created using the new keyword followed by the class constructor. Syntax: ClassName objectName = new ClassName(arguments); objectName: The name of the object. arguments: Values passed to the constructor to initialize the object.

  4. class Car { // Fields String color; int year; // Constructor Car(String color, int year) { this.color = color; this.year = year; }

  5. // Method void displayInfo() { System.out.println("Car color: " + color + ", Year: " + year); } } public class Main { public static void main(String[] args) { // Creating an object Car myCar = new Car("Red", 2020); myCar.displayInfo(); // Output: Car color: Red, Year: 2020 } }

  6. Strings String is the type of objects that can store the sequence of characters enclosed by double quotes and every character is stored in 16 bits i.e using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowing for various operations such as concatenation, comparison, and manipulation. Example: String name = HAI ; String num = 1234

  7. Example public class Geeks { // Main Function public static void main(String args[]) { // creating Java string using new keyword String str = new String("GeeksforGeeks"); System.out.println(str); } }

  8. Inheritance Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.

  9. Important Terminologies Used in Java Inheritance Class: Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created. Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class). Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

  10. Reusability: Inheritance supports the concept of reusability, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

Related


More Related Content