S.VIGNESH Assistant Professor/CSE, SECE

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Object and Class in Java. Java Naming conventions A naming convention is a rule to follow as you decide what to name your identifiers e.g. class,
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Java Programming static keyword.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Methods.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Staples are our staple Building upon our solution.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Java Methods and Applications CSIS 3701: Advanced Object Oriented Programming.
Constructors and Destructors
Chapter No. : 2 Classes and Objects.
Chapter 7 Objects and Classes
GC211 Data structure Lecture 3 Sara Alhajjam.
Lecture 3: Introduction to Object and Classes
3 Introduction to Classes and Objects.
Static data members Constructors and Destructors
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
Object Initialization and Clean up
Coming up Constructors Overloading With one parameter
Review Session.
INHERITANCE IN JAVA.
Constructor & Destructor
Week 8 Lecture -3 Inheritance and Polymorphism
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Introduction to Java.
CSC 113 Tutorial QUIZ I.
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Contents Introduction to Constructor Characteristics of Constructor
Sampath Kumar S Assistant Professor, SECE
Encapsulation and Constructors
Chapter 8 Objects and Classes Part 1
CLASS DEFINITION (FIELDS)
Chapter 9 Objects and Classes
Object Oriented Programming in java
Unit-2 Objects and Classes
Assignment 7 User Defined Classes Part 2
Constructors and Destructors
S.VIGNESH Assistant Professor, SECE
Method Overloading in JAVA
Method Overriding in Java
Chapter 8 Objects and Classes
JAVA Constructors.
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Inheritance with Constructor
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Sampath Kumar S Assistant Professor, SECE
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
Chapter 11 Inheritance and Polymorphism Part 1
OO Programming Concepts
Constructors & Destructors
Chapter 7 Objects and Classes
Presentation transcript:

S.VIGNESH Assistant Professor/CSE, SECE Constructor in Java 01-01-2019 S.VIGNESH, AP/CSE

What is Method Constructor? Constructor is a special type of method that is used to initialize the object. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor. 01-01-2019 S.VIGNESH, AP/CSE

Rules for creating constructor There are basically two rules defined for the constructor. Constructor name must be same as its class name Constructor must have no explicit return type 01-01-2019 S.VIGNESH, AP/CSE

Types of constructors There are two types of constructors: Default constructor (no-arg constructor) Parameterized constructor Note: If there is no constructor in a class, compiler automatically creates a default constructor. 01-01-2019 S.VIGNESH, AP/CSE

1. Default Constructor A constructor that have no parameter is known as default constructor. Syntax of default constructor: <class_name>(){}   Example: class Bike{   Bike(){ System.out.println("Bike is created");}     public static void main(String args[]){   Bike b=new Bike();   }   Output: Bike is created 01-01-2019 S.VIGNESH, AP/CSE

Purpose of default constructor? Default constructor provides the default values to the object like 0, null etc. depending on the type. Example: class Student{   int id;  String name;     void display(){ System.out.println(id+" "+name);}   public static void main(String args[]){   Student s1=new Student(),s2=new Student();   s1.display();   s2.display();   }   Output : 0 null 01-01-2019 S.VIGNESH, AP/CSE

2. Parameterized Constructor Parameterized constructor is used to provide different values to the distinct objects. Syntax of Parameterized constructor: <class_name>(){ <class_name>(args1,…args n) } 01-01-2019 S.VIGNESH, AP/CSE

Example class Student{ } void display(){     int id;       String name;       Student(int i,String n){       id = i;        name = n;        }       void display(){ System.out.println(id+" "+name);}       public static void main(String args[]){        Student s1 = new Student(111,"Karan");        Student s2 = new Student(222,"Aryan");        s1.display();        s2.display();      }   }   Output: 111 Karan 222 Aryan 01-01-2019 S.VIGNESH, AP/CSE

Constructor Overloading Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists. The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.   01-01-2019 S.VIGNESH, AP/CSE

Example Output: 111 Karan 0 222 Aryan 25 class Student{ int id; String name; int age; Student(int i,String n){ id = i; name = n; } Student(int i,String n,int a){ age=a; void display(){ System.out.println(id+" "+name+" "+age); } public static void main(String args[]){ Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan",25); s1.display(); s2.display(); } } Output: 111 Karan 0 222 Aryan 25 01-01-2019 S.VIGNESH, AP/CSE

JAVA CONSTRUCTOR JAVA METHOD It is used to initialize the state of an object. It is used to expose the behavior of an object. It must not have an return type. It must have return type. It is invoked implicitly. It is invoked explicitly. The java compiler provides a default constructor. Method is not provided by compiler in any case. Constructor name must be same as the class name. Method name may or may not be same as class name. 01-01-2019 S.VIGNESH, AP/CSE

01-01-2019 S.VIGNESH, AP/CSE

Thank You  01-01-2019 S.VIGNESH, AP/CSE