Definning class
• A class in the context of Java is a template used to
create objects and to define object data types and
methods. Classes are categories, and objects are items
within each category. All class objects should have
the basic class properties.
What is class in Java with Example
program
• Example: A dog has states - color, name, breed
as well as behaviors – wagging the tail,
barking, eating. An object is an instance of a
class. Class − A class can be defined as a
template/blueprint that describes the
behavior/state that the object of its type
support
How do you define a new class?
• Creating a new class creates a new type of
object, allowing new instances of that type to
be made. Each class instance can have
attributes attached to it for maintaining its
state. Class instances can also have methods
(defined by its class) for modifying its state.
What is class and example
• A class is a group of objects that share
common properties and behavior. For example,
we can consider a car as a class that has
characteristics like steering wheels, seats,
brakes, etc.
JAVA C0NSTRUCTOR
• A constructor in Java is a special method that
is used to initialize objects. The constructor is
called when an object of a class is created. It
can be used to set initial values for object
attributes.
SYNTAX
• class Geek
• { ....... // A Constructor new Geek()
• { } .......
• }
• import java.io.*;
• class Geeks
• {
• Geeks()
• {
• super();
• }
• public static void main(String[] args)
• {
• Geeks geek = new Geeks();
• }
• }
Types of Constructors in Java
• No-argument constructor
• Parameterized Constructor
• No-argument constructor: A constructor that has
no parameter is known as the default constructor.
If we don’t define a constructor in a class, then
the compiler creates a default constructor(with no
arguments) for the class. And if we write a
constructor with arguments or no arguments then
the compiler does not create a default
constructor.
• import java.io.*;
• class Geek {
• int num;
• String name;
• Geek() { System.out.println("Constructor called"); }
• }
•
• class GFG {
• public static void main(String[] args)
• {
• // this would invoke default constructor.
• Geek geek1 = new Geek();
• // Default constructor provides the default
• // values to the object like 0, null
• System.out.println(geek1.name);
• System.out.println(geek1.num);
• }
• }
• Constructor called
• null 0
• Parameterized Constructor: A constructor that
has parameters is known as parameterized
constructor. If we want to initialize fields of
the class with our own values, then use a
parameterized constructor
• import java.io.*;
•
• // Class 1
• class Geek {
• // data members of the class.
• String name;
• int id;
•
• // Constructor would initialize data members
• // With the values of passed arguments while
• // Object of that class created
• Geek(String name, int id)
• {
• this.name = name;
• this.id = id;
• }
• }
• class GFG {
• // main driver method
• public static void main(String[] args)
• {
• // This would invoke the parameterized
constructor.
• Geek geek1 = new Geek("adam", 1);
• System.out.println("GeekName :" + geek1.name
• + " and GeekId :" + geek1.id);
• }
• }
• Output
• GeekName :adam and GeekId :1

Definning class.pptx unit 3

  • 1.
    Definning class • Aclass in the context of Java is a template used to create objects and to define object data types and methods. Classes are categories, and objects are items within each category. All class objects should have the basic class properties.
  • 2.
    What is classin Java with Example program • Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class. Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support
  • 3.
    How do youdefine a new class? • Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
  • 4.
    What is classand example • A class is a group of objects that share common properties and behavior. For example, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc.
  • 5.
    JAVA C0NSTRUCTOR • Aconstructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.
  • 6.
    SYNTAX • class Geek •{ ....... // A Constructor new Geek() • { } ....... • }
  • 7.
    • import java.io.*; •class Geeks • { • Geeks() • { • super(); • } • public static void main(String[] args) • { • Geeks geek = new Geeks(); • } • }
  • 8.
    Types of Constructorsin Java • No-argument constructor • Parameterized Constructor • No-argument constructor: A constructor that has no parameter is known as the default constructor. If we don’t define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class. And if we write a constructor with arguments or no arguments then the compiler does not create a default constructor.
  • 9.
    • import java.io.*; •class Geek { • int num; • String name; • Geek() { System.out.println("Constructor called"); } • } • • class GFG { • public static void main(String[] args) • { • // this would invoke default constructor. • Geek geek1 = new Geek(); • // Default constructor provides the default • // values to the object like 0, null • System.out.println(geek1.name); • System.out.println(geek1.num); • } • }
  • 10.
  • 11.
    • Parameterized Constructor:A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor
  • 12.
    • import java.io.*; • •// Class 1 • class Geek { • // data members of the class. • String name; • int id; • • // Constructor would initialize data members • // With the values of passed arguments while • // Object of that class created • Geek(String name, int id) • { • this.name = name; • this.id = id; • } • }
  • 13.
    • class GFG{ • // main driver method • public static void main(String[] args) • { • // This would invoke the parameterized constructor. • Geek geek1 = new Geek("adam", 1); • System.out.println("GeekName :" + geek1.name • + " and GeekId :" + geek1.id); • } • }
  • 14.
    • Output • GeekName:adam and GeekId :1