Vous êtes sur la page 1sur 11

Lansigan, Trisha Joy T. Mr. Mark Anthony S.

Mercado

ICT 12 – Bl.Avelino Computer Programming

PACKAGE – Package in Java is a mechanism to encapsulate a group of classes, sub packages


and interfaces. Packages are used for preventing naming conflicts, Making searching/locating
and usage of classes, interfaces, enumerations and annotations easier, providing controlled access
and packages can be considered as data encapsulation or data-hiding.

SYNTAX : import package.package_name.*;


import package_name;

METHOD – A method is a set of code which is referred to by name and can be called at any
point in a program simply by utilizing the method's name. A method must be declared within a
class. It is defined with the name of the method, followed by parentheses (). Java provides some
pre-defined methods, such as System.out.println().

SYNTAX : public class Class_Name {


public static method_name() {
valid_statement;
}
}

PARAMETER – A parameter or a formal argument, is a special kind of variable, used in a


subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces
of data are the values of the arguments with which the subroutine is going to be called/invoked.

SYNTAX : public class Class_Name {


public static method_name( parameter/s ) {
valid_statement;
}
}
CLASS – A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one type.

SYNTAX : public class Class_Name {


valid_statement;
}

OBJECT – It is a basic unit of Object Oriented Programming and represents the real life
entities. A typical Java program creates many objects, which as you know, interact by invoking
methods.

SYNTAX : public class Class_Name {


public static void main (String [] args) {
Class_Name object_name = new Class_Name();
valid_statement;
}
}

4 KINDS OF METHOD

METHOD WITHOUT PARAMETER AND WITHOUT RETURN VALUE – When a


method has no parameters. It does not receive any data from the tha calling method. Similarly
when it does not return a value, the calling method does not receive any data from the called
method.

SYNTAX : public void method_name () {


valid_statement;
}
EXAMPLE : public void trisha () {
System.out.println(“Hello I am Trisha”);
}
METHOD WITH PARAMETER AND WITHOUT RETURN VALUE – When a method
has parameters it recieves any data from the calling method but it has no return value.

SYNTAX : public void method_name(parameter/s) {


valid_statement;
}

EXAMPLE: public void trisha (String name) {


System.out.println(“Hello I am”+name);
}

METHOD WITH PARAMETER AND WITH RETURN VALUE – When a method has
parameters. It receives data from the tha calling method and it also returns a value, the method
that’s being called receive data from the called method.

SYNTAX : public string method_name(parameter/s) {


valid_statement;
return;
}

EXAMPLE : public string trisha (int age) {


System.out.println(“I am”+age+“years old”);
return;
}

METHOD WITHOUT PARAMETER AND WITH RETURN VALUE – When a method


has no parameter. It does not revieve any data from the calling method. But it returns a value, the
calling method does not receive any data from the called method.

SYNTAX : public string method_name() {


valid_statement;
return;
}

EXAMPLE: public string trisha () {


System.out.println(“I am 18 years old”);
return;
}
STATE – It is represented by attributes of an object. It also reflects the properties of an object.

SYNTAX : class State_Name{

valid_statement;

BEHAVIOR – It is represented by methods of an object. It also reflects the response of an


object with other objects.

SYNTAX : class State_Name {


public static void main (String [] args) {
State_Name object_name = new State_Name(parameter/s);
}

FIELD – A java field is a variable declared as part of class, so that each instance of that class
containts an instance of that variable. A field can be public static, not static and final.

SYNTAX : public class Class_Name {

[access_modifier] [static] [final] type name [= initial value] ;

SETTER – A setter in java is a method that updates the value of a variable.Setter is also setting
a new field.
SYNTAX : public void setMethod_Name(parameter/s) {

valid_statement;

GETTER – A getter in java is a mehod that reads the value of the variable. Getter is also a
method that gets a private field.

SYNTAX : public String getMethod_Name() {

valid_statement;
}

CONSTRUCTOR – Constructor is a block of code that initializes the newly created object. A
constructor resembles an instance method in java but it’s not a method as it doesn’t have a return
type. In short constructor and method are different. People often refer constructor as special type
of method in Java.

SYNTAX : public class Subclass_Name{


//This is the constructor
Method_Name(){
valid_statement;
}
}

EXAMPLE : public class Hello {


String name;
//Constructor
Hello(){
this.name = "Ishang";
}
public static void main(String[] args) {
Hello object = new Hello();
System.out.println(object.name);
}
}

ENCAPSULATION – Encapsulation is one of the four fundamental OOP concepts.


Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the
data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden
from other classes, and can be accessed only through the methods of their current class.
Therefore, encapsulation also known as data hiding.
SYNTAX : public class Subclass_Name{
private Superclass_Name;
public String getMethod_Name(){
return Superclass_Name;
}
public void setMethod_Name(parameter/s){
this.Superclass_Name = Superclass_Name
}
}

class Subclass_name{
public static void main(String[] args){
Subclass_name object_name = new Subclass_name();
object_name.setMethod_Name("valid_statement");
System.out.println(object_name.getMethod_Name());
}
}

EXAMPLE : public class Student{


private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name
}
}
class Test{
public static void main(String[] args){
Student avelino =new Student();
avelino.setName("Ishang");
System.out.println(avelino.getName());
}
}

INHERITANCE – Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented
programming system). The idea behind inheritance in Java is that you can create new classes that
are built upon existing classes. When you inherit from an existing class, you can reuse methods
and fields of the parent class. Moreover, you can add new methods and fields in your current
class also. Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.

SYNTAX : class Subclass_name extends Superclass_name {


//methods and fields
}

EXAMPLE : class Employee{


float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

POLYMORPHISM – Polymorphism in Java is a concept by which we can perform a single


action in different ways. polymorphism means many forms. here are two types of polymorphism
in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism
in java by method overloading and method overriding.

SYNTAX : class Subclass_Name{}


class Superclass_Name extends Subclass_Name{
valid_state;
}
Subclass_Name object_name = new Superclass_Name();//upcasting

EXAMPLE : class Bike{


void run(){
System.out.println("running");
}
}
class Splendor extends Bike{
void run(){
System.out.println("running safely with 60km");
}
public static void main(String args[]){
Bike b = new Splendor();//upcasting
b.run();
}
}

METHOD OVERLOADING – Method Overloading is a feature that allows a class to have


more than one method having the same name, if their argument lists are different. It is similar to
constructor overloading in Java, that allows a class to have more than one constructor having
different argument lists.

SYNTAX : void method_name() {


valid_statement
}
void method_name(parameter/s) {
valid_statement;
}
method_name(parameter/s) {
valid_statement;
}
method_name(parameter/s) {
valid_statement; }
EXAMPLE : class MethodOverloading {
// this method accepts int
private static void display(int a){
System.out.println("Got Integer data.");
}
// this method accepts String object
private static void display(String a){
System.out.println("Got String object.");
}
public static void main(String[] args) {
display(1);
display("Hello");
}
}

METHOD OVERRIDING – In any object-oriented programming language, Overriding is a


feature that allows a subclass or child class to provide a specific implementation of a method that
is already provided by one of its super-classes or parent classes. When a method in a subclass has
the same name, same parameters or signature and same return type(or sub-type) as a method in
its super-class, then the method in the subclass is said to override the method in the super-class.

SYNTAX : class Subclass_Name{


public void method_name() {
valid_statement;
}
}
class Superclass_Name extends Subclass_Name{
public void method_name(){
valid_statement;
}
public static void main( String args[]) {
Superclass_Name object_name = new Superclass_Name();
object_name.method_name();
}
}

EXAMPLE : class Human{


public void eat() {
System.out.println("Human is eating");
}
}
class Boy extends Human{
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
obj.eat();
}
}

ABSTRACTION – Abstraction is a process of hiding the implementation details from the user,
only the functionality will be provided to the user. In other words, the user will have the
information on what the object does instead of how it does it. Abstraction can be achieved with
either abstract classes or interfaces.

SYNTAX : abstract class Class_Name {


public abstract void method_name();
public void method_name() {
valid_statement;
}
}
EXAMPLE : abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}

Vous aimerez peut-être aussi