Java For Android Tutorial #17: Oop concepts - Classes and Objects
Code:
package com.example;
public class java {
public static void main(String[] str) {
BioData johnBioData = new BioData("John Zeller","11 West Way Street",21,7462718);
johnBioData.showEverything();
BioData alisaBioData = new BioData("Alisa Johnson","11 North Street",23,4857283);
alisaBioData.showEverything();
}
}
class BioData{
String name,address;
int age,phone;
public BioData(String pName,String pAddress,int pAge,int pPhone){
name = pName;
address = pAddress;
age = pAge;
phone = pPhone;
}
public void showEverything(){
showName();
showAddress();
showAge();
showPhone();
} public void showName(){
System.out.println("Person Name is: "+ name);
}
public void showAddress(){
System.out.println("Person Address is: "+ address);
}
public void showAge(){
System.out.println("Person Age is: "+ age);
}
public void showPhone(){
System.out.println("Person Phone Number is: "+ phone);
}
}In this code, I have explained the important concept of classes and objects in java. For that, I implemented one program with the example of biodata. As strings variables, I have considered name and address. As integers variables, I have age and phone. In the program, there are 2 classes. One is the default class, which only have a main method. And other is the Biodata class that has 4 variables and 6 methods. 1 is the constructor, 4 methods are used to display the data and 1 method is used to call the previous 4 methods. I have made 2 objects with 2 different datasets.
Comments
Post a Comment