Pundra University of Science & Technology Department of Computer Science & Engineering Name: Md. Sihab Uddin Batch No: 6th Semester No: 4th Roll No: 05
Outline • Program Structure • Concept of Class • Concept of Object • Concept of Constructor • Method
Program Structure • Structure Import Statement Package Statement Class Definition Inheritance Statement Interface Statement Main Method
Concept of Class • A class is simply a representation of a type of object • It is the blueprint/ plan/ template that describe the details of an object • For example- mango, apple, orange are the members of the class fruits • If fruits has been defined as a class, then the statement Fruits mango; • It will create an object mango belonging to the class fruit
Cont…….. • Java is a case-sensitive language • Class declaration • Opening braces • The main line • The output line Class Csepu { Public static void main (String args[]) { System.out.println(“Java is better than C++”); } }
Cont…………… • Class declaration Class Csepu – This line uses the keyword class to declare that a new class is being defined – Csepu is the name of the class • Opening braces { …… } – The class definition begins with the opening curly brace ({) and ends with the closing curly brace (}). – The elements between the two braces are members of the class
Cont……….. • The main line Public static void main (String args[]) – The keyword public means that the method main() can be accessed by any other Java class. – The keyword static means that you don’t have to create an instance of this class to use this method. – The keyword void means that the method main() doesn’t return any value to the calling program. – The keyword String[] args tells us that this method will receive an array of characters as the argument
Cont……. • The Output line System.out.println(“Java is better than C++”); – This is similar to the printf() statement of C – Since Java is a true object oriented language, every method must be part of an object – The println method is a member of the out object, which is a static data member of System class – The method println always appends a newline character to the end of the string – This means that any subsequent output will start on a new line
Object • Objects are instances of a class. It is the building block of OOP. • They are the basic runtime entities in an object oriented system • They may present a person, a place, a bank account, a table of data or any item that the program may handle • Object must have the following three characteristics: – Identity – State – Behavior
Concept of Constructors • A constructor initializes an object when it is created • It has the same name as its class and is syntactically similar to a method • However, constructors have no explicit return type • simple example class Adition { public void add() {int a,b,add; a=4;b=5; add=a+b; System.out.println("Result of Add: "+add); } } public class Exmcons1 { public static void main (String args[]) { Adition obj = new Adition(); obj.add(); } }
Concept of Methods • Methods are the interface or communications between program components • A Java method is a collection of statements that are grouped together to perform an operation • As an example when we call the System.out.println method… – The system actually executes several statements in order to display a message on the console – In general, a method has the following syntax Modifier returnValueType methodName (list of parameters) { //method of body; }
Cont…………..
Declaring a Methods • Modifiers – The modifier, which is optional, tells the compiler how to call the method – This defines the access type of the method • Return Type – A method may return a value – The returnValueType is the data type of the value the method returns – Some methods perform the desired operations without returning a value – In this case, the returnValueType is the keyword void
Cont…………. • Method Name – This is the actual name of the method – The method name and the parameter list together constitute the method signature • Parameters – A parameter is like a place holder – When a method is invoked, pass a value to the parameter – This value is referred to as actual parameter or argument – The parameter list refers to the type, order, and number of the parameters of a method – Parameters are optional; that is, a method may contain no parameters
Cont…………. • Method Body – The method body contains a collection of statements that define what the method does • Example – The following defined method called max() – This method takes two parameters num1 and num2 and returns the maximum between the two public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }

Java class,object,method introduction

  • 1.
    Pundra University ofScience & Technology Department of Computer Science & Engineering Name: Md. Sihab Uddin Batch No: 6th Semester No: 4th Roll No: 05
  • 2.
    Outline • Program Structure •Concept of Class • Concept of Object • Concept of Constructor • Method
  • 3.
    Program Structure • Structure ImportStatement Package Statement Class Definition Inheritance Statement Interface Statement Main Method
  • 4.
    Concept of Class •A class is simply a representation of a type of object • It is the blueprint/ plan/ template that describe the details of an object • For example- mango, apple, orange are the members of the class fruits • If fruits has been defined as a class, then the statement Fruits mango; • It will create an object mango belonging to the class fruit
  • 5.
    Cont…….. • Java isa case-sensitive language • Class declaration • Opening braces • The main line • The output line Class Csepu { Public static void main (String args[]) { System.out.println(“Java is better than C++”); } }
  • 6.
    Cont…………… • Class declaration ClassCsepu – This line uses the keyword class to declare that a new class is being defined – Csepu is the name of the class • Opening braces { …… } – The class definition begins with the opening curly brace ({) and ends with the closing curly brace (}). – The elements between the two braces are members of the class
  • 7.
    Cont……….. • The mainline Public static void main (String args[]) – The keyword public means that the method main() can be accessed by any other Java class. – The keyword static means that you don’t have to create an instance of this class to use this method. – The keyword void means that the method main() doesn’t return any value to the calling program. – The keyword String[] args tells us that this method will receive an array of characters as the argument
  • 8.
    Cont……. • The Outputline System.out.println(“Java is better than C++”); – This is similar to the printf() statement of C – Since Java is a true object oriented language, every method must be part of an object – The println method is a member of the out object, which is a static data member of System class – The method println always appends a newline character to the end of the string – This means that any subsequent output will start on a new line
  • 9.
    Object • Objects areinstances of a class. It is the building block of OOP. • They are the basic runtime entities in an object oriented system • They may present a person, a place, a bank account, a table of data or any item that the program may handle • Object must have the following three characteristics: – Identity – State – Behavior
  • 10.
    Concept of Constructors •A constructor initializes an object when it is created • It has the same name as its class and is syntactically similar to a method • However, constructors have no explicit return type • simple example class Adition { public void add() {int a,b,add; a=4;b=5; add=a+b; System.out.println("Result of Add: "+add); } } public class Exmcons1 { public static void main (String args[]) { Adition obj = new Adition(); obj.add(); } }
  • 11.
    Concept of Methods •Methods are the interface or communications between program components • A Java method is a collection of statements that are grouped together to perform an operation • As an example when we call the System.out.println method… – The system actually executes several statements in order to display a message on the console – In general, a method has the following syntax Modifier returnValueType methodName (list of parameters) { //method of body; }
  • 12.
  • 13.
    Declaring a Methods •Modifiers – The modifier, which is optional, tells the compiler how to call the method – This defines the access type of the method • Return Type – A method may return a value – The returnValueType is the data type of the value the method returns – Some methods perform the desired operations without returning a value – In this case, the returnValueType is the keyword void
  • 14.
    Cont…………. • Method Name –This is the actual name of the method – The method name and the parameter list together constitute the method signature • Parameters – A parameter is like a place holder – When a method is invoked, pass a value to the parameter – This value is referred to as actual parameter or argument – The parameter list refers to the type, order, and number of the parameters of a method – Parameters are optional; that is, a method may contain no parameters
  • 15.
    Cont…………. • Method Body –The method body contains a collection of statements that define what the method does • Example – The following defined method called max() – This method takes two parameters num1 and num2 and returns the maximum between the two public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }