Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

Members

Abdullahi Ali Abdullahi 21/1958/BIT-J


Abdullahi Mohamed Abdi 21/1917/BIT-J
Shueb Said Dirir 21/2026/BIT-J
Mohamed Abdullahi Hassan
21/952/BSSE-J
Osman Ahmed Aden 21//1267/BSCS-J
Classes in java
A class is a group of objects which have common
properties. It is a template or blueprint from which objects
are created. It is a logical entity. It can't be physical. A
class in Java can contain:
 Fields
 Methods
Constructors
Blocks
 Nested class and interface
Cont . . .
A class can contain any of the following variable types.
Local variables: Variables defined inside methods, constructors or
blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed when
the method has completed.
Instance variables: Instance variables are variables within a class but
outside any method. These variables are initialized when the class is
instantiated. Instance variables can be accessed from inside any
method, constructor or blocks of that particular class.
 Class variables: Class variables are variables declared with in a class,
outside any method, with the static keyword. A class can have any
number of methods to access the value of various kinds of methods. In
the above example, barking, hungry and sleeping are methods.
Constructors

 When discussing about classes, one of the most important


sub topic would be constructors. Every class has a
constructor. If we do not explicitly write a constructor for a
class the Java compiler builds a default constructor for that
class. Each time a new object is created, at least one
constructor will be invoked. The main rule of constructors
is that they should have the same name as the class.
 A class can have more than one constructor.
Syntax of class in java
Class (class_Name){
field;
method;
}

Forexample

Public class Lecturer{


String firstName;
String lastName;
int tellNumber;

}
Object in java
An entity that has state and behavior is known as an
object
An object has three characteristics:
 State: represents the data (value) of an object.
 Behavior: represents the behavior (functionality) of an
object such as deposit,etc.
 Identity: An object identity is typically implemented
via a unique ID.
 Difference between object and class
An object in Java is the physical as well as a logical
entity, whereas, a class in Java is a logical entity only
Syntax of object in java
In our previous example, we had created a lecturer class which has
three data members first name, last name and id. We are creating
the object of the lecturer class by new keyword.
Public class Demo{
Public static void main(String[]args){
lecturer business= new lecturer();
business.firstName=“john”;
business.lastName=“ben”;
business. tellNumbe=90087945;

}
}
Examole1
public class player {
int playerno;
String firstname;
String lastname;
String nationality;
String playerteam;
 
public player(int pno, String fname, String lname, String natio, String pteam){
playerno=pno;
firstname=fname;
lastname=lname;
nationality=natio;
playerteam=pteam;
}
public void display(){
System.out.println("the player number is "+playerno);
System.out.println("the first name is "+firstname);
System.out.println("the lastname is "+lastname);
System.out.println("the nationality is"+nationality);
System.out.println("the program is "+playerteam);
}
public static void main(String[] args) {
player player1 = new player(19, "sadio", "mane", "senegal", "bayern
munich");
player1.display();
System.out.println( " ");
player player2=new player(7, "cristiano", "Ronaldo", "Portugal", "Man
united");
player2.display();
}
}
example2

public class student {


 String regno;
 String firstname;
 String lastname;
 String program;

 public student(String r, String fname, String lname, String pr) {


 regno = r;
 firstname = fname;
 lastname = lname;
 program = pr;
 }
 public void display(){
 System.out.println("the registration number is"+regno);
 System.out.println("the first name is"+firstname);
 System.out.println("the lastname is"+lastname);
 System.out.println("the program is"+program);
 System.out.println(" ");

public static void main(String[] args) {


 student stud1 = new student("21/188/bit", "abdullahi", "ALi", "bit");
 stud1.display();

 student stud2=new student("12/44/67", "shuceyb", "geedi", "bsse");


 stud2.display();
 }
}
Example3
ublic class vehicle {
 String regno;
 String model;
 String color;
 String enginenumber;

 public vehicle(String r, String mo, String co, String en) {


 regno = r;
 model = mo;
 color = co;
 enginenumber = en;
 }
 public void display(){
 System.out.println("the registration number is "+regno);
 System.out.println("the model is " + model);
 System.out.println("the color is " +color);
 System.out.println("the engine number is " +enginenumber);

 System.out.println(" ");

 }

 public static void main(String[] args) {


 vehicle veh1 = new vehicle("1SAM123", "lamborgini",
"black","52WTY53283");
 veh1.display();

 vehicle veh2=new vehicle("ieu263", "buggati", "GREEN", "8264ygw");


 veh2.display();

You might also like