Object Oriented in Java with basic inheritance example
Design based on OOP approach with Person and Faculty classes. Person
class be implemented to PersonDemo and Person class be inherited to Faculty class. InheritanceDemo will perform the action to display the output. We
all know that OOP is the best practice when designing a software that
developers/programmers can benefits the reuse of existing codes, re-write the
same functions for different situations, ease in software maintenance, and a
lot more.
Person
/** * Created by joannnou on 22 Mar 2017. */public class Person { private String firstName,lastName,middleName,gender; private int age; public Person(String fName, String lName, String mName, String gender, int age){ this.firstName = fName; this.lastName = lName; this.middleName = mName; this.gender = gender; this.age = age; } public Person(){ this.firstName = "No firstname yet!"; this.lastName = "No lastnname yet!"; this.middleName ="No middlename yet!"; this.gender = "No gender yet"; this.age = 0; } public Person(String fName, String lName, String mName){ this.firstName = fName; this.lastName = lName; this.middleName = mName; } //set firstname public void setFirstName(String fName){ this.firstName = fName; } //get firstname public String getFirstName(){ return firstName; } //set lastname public void setLastName(String lastName) { this.lastName = lastName; } //get lastname public String getLastName() { return lastName; } //set middlename public void setMiddleName(String mName){ this.middleName = mName; } //get middlename public String getMiddleName(){ return middleName; } //set gender public void setGender(String gender) { this.gender = gender; } //get gender public String getGender() { return gender; } //get name format public String fullName(){ return "Name:" + firstName + " " + middleName + ". " + lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\'' + ", middleName='" + middleName +'\''+ ", gender='" + gender + '\'' + ", age=" + age + '}'; } }
PersonDemo
/** * Created by joannnou on 22 Mar 2017. */
import java.util.Scanner;
public class PersonDemo {
static Scanner console = new Scanner(System.in);
public static void main(String[]args){
Person p = new Person();
String fName,lName,mName,gender;
int age;
System.out.println("Enter your firstname:");
fName = console.nextLine();
p.setFirstName(fName);
System.out.println("Enter your middlename:");
mName = console.nextLine();
p.setMiddleName(mName);
System.out.println("Enter your lastname:");
lName = console.nextLine();
p.setLastName(lName);
System.out.println("Enter your gender");
gender = console.nextLine();
p.setGender(gender);
System.out.println("Enter your age:");
age = console.nextInt();
p.setAge(age);
//display all detail information
System.out.println(p.toString()); //display name only
System.out.println(p.fullName());
}
}
Faculty
/** * Created by joannnou on 22 Mar 2017. */
public class Faculty extends Person {
private String position; public Faculty(){ super(); position = "No Position Yet!"; } public Faculty(String facFirstName,String facMiddleName, String facLastName, String facGender, int facAge, String facPosition){ super(facFirstName,facMiddleName,facLastName,facGender,facAge); this.position = facPosition; } public void setPosition(String facultyPosition){ this.position = facultyPosition; } public String getPosition(){ return position; } public String toStringFac(){ //toString method came from Person class
//I rename toStringFac for Faculty class
return toString() + "\nPosition:" + position; } }
InheritanceDemo
/** * Created by joannnou on 22 Mar 2017. */
public class InheritanceDemo { public static void main(String []arg){ Faculty initialName = new Faculty(); Faculty assignName = new Faculty("Joannou","H.", "Fegarido","Male",29,"Faculty"); //display no name yet!
//get the default value from Person class
System.out.println(initialName.toStringFac()); System.out.println("--------------------------------"); //display assigned name
System.out.println(assignName.toStringFac());
}
}
Output:
PersonDemo
produced the following output in Line 36:
Person{firstName='Joannou',
lastName='Fegarido', middleName='H', gender='Male', age=29}
Name:Joannou H.
Fegarido
InheritanceDemo
produced the following output:
Person{firstName='No
firstname yet!', lastName='No lastnname yet!', middleName='No middlename yet!',
gender='No gender yet', age=0}
Position:No
Position Yet!
--------------------------------
Person{firstName='Joannou',
lastName='H.', middleName='Fegarido', gender='Male', age=29}
Position:Faculty