Java Object Oriented Programming Part I

In this article series, I will try to cover Java Object Oriented Programming concepts in 3 parts. In this article, I will cover the below bolded features.

  • Class & Object, Constructor, Attributes, Methods
  • Encapsulation
  • Access Modifiers (Public, Private, Protected)
  • Static Attributes and Methods
  • Final Attributes
  • Inheritence
  • Polymorphism
  • Interfaces and Abstract Classes

Below codes explains about Class, Object, Attributes, Constructor, Methods, Getters and Setters

Create the class CourseFrameworks.Java as shown below. Here I have created two private attributes called coureName and frameworkName, Constructor, Getters and Setters

To achieve Encapsulation, declare class attributes as private and define public setter and getter methods to access and update the value of a private attributes.

Here we have added parameterized Constructor. A constructor is a special type of method used to initialize objects. The constructor is automatically called when an object of a class is created.

public class CourseFrameworks {
		 
	private String courseName;  // Attribute
	private String frameworkName;	// Attribute
 
        //Constructor
        public CourseFrameworks(String courseN, String frameworkN) {
           this.courseName = courseN;
           this.frameworkName = frameworkN;
        }
	
	// Getter
	public String getcourseName() {
		return courseName;
	}

	// Setter
	public void setcourseName(String newCourseName) {
		this.courseName = newCourseName;
	}
  
	// Getter
	public String getframeworkName() {
		return frameworkName;
	}

	// Setter
	public void setframeworkName(String newFrameworkName) {
		this.frameworkName = newFrameworkName;
	}
       
        // Method
          public  void display(){
             System.out.println("Course Name = "+this.courseName);
             System.out.println("Framework Name = "+this.frameworkName);

     }
}

Now create the class MyFirstClass.java as shown below

public class MyFirstClass {

	public static void main(String[] args) {
        // Class Object Created and Initialized the values through Class Constructor
        CourseFrameworks cf = new CourseFrameworks("Java", "Spring Framework");
	
        // Invoked the display method of CourseFrameworks Class
        cf.display();

	}
}
Course Name = Java
Framework Name = Spring Framework

Suppose if you want to update the values of the class attributes with Setters and Getters, do the below. He we set the course name and framework name with setcourseName() and setframeworkName() properties.

public class MyFirstClass {

	public static void main(String[] args) {
        
        CourseFrameworks cf= new CourseFrameworks("Python", "Django2");

	cf.setcourseName("Java Advanced");
	cf.setframeworkName("Spring Framework");
	
        System.out.println("CourseName: " + cf.getcourseName() + " " + "FrameworkName: " + cf.getframeworkName());

          cf.display();

	}
}
CourseName: Java Advanced FrameworkName: Spring Framework
Course Name = Java Advanced
Framework Name = Spring Framework

Static Attributes and Methods

Static Attributes and methods belongs to the class, rather than an object. Non-static variables cannot be referenced from a static context.

Add the below code to CourseFrameworks.java file

        static int fee = 500;   // Static Attribute
        static String author = "Girish";  // Static Attribute

       //Here static attribute fee increments everytime the class object is created.
       public CourseFrameworks(String courseN, String frameworkN) {
		this.courseName = courseN;
		this.frameworkName = frameworkN;
		fee++;
	}
   
        // Static Method
	public static void showFee() {
		System.out.println("Value of fee = " + fee);
	}

        // Static Method
	public static void showAuthor() {
		System.out.println("Author Name: " + author);
	}

And add the below code to MyFirstClass.java file

CourseFrameworks cf= new CourseFrameworks("Python", "Django2");
		System.out.println(
				"CourseName: " + cf.getcourseName() + " " + "FrameworkName: " + cf.getframeworkName());
		CourseFrameworks.showFee(); // Static Method can be accessed without creating an object of the class
		CourseFrameworks.showAuthor(); // Accessing Static Method
		CourseFrameworks.author = "Girish updated"; // Updating Static Attribute Value
		System.out.println("\n");

		CourseFrameworks cf1= new CourseFrameworks("Python", "Django2");

		cf1.setcourseName("Java");
		cf1.setframeworkName("Spring Framework");
		System.out.println(
				"CourseName: " + cf1.getcourseName() + " " + "FrameworkName: " + cf1.getframeworkName());
		CourseFrameworks.showFee();
		CourseFrameworks.showAuthor();

Once you save both the class files and run the code, you can see the below output. If you observe, I have addedfee++ inside constructor, everytime the class object is created the value of fee is incremented. Here Static Methods showFee() and showAuthor() can be accessed without creating an object of the class

CourseName: Python FrameworkName: Django2
Value of fee = 502
Author Name: Girish


CourseName: Java FrameworkName: Spring Framework
Value of fee = 503
Author Name: Girish updated

final – Class, Attributes and Methods

  • final class cannot be inherited by other classes
  • final Attributes and methods cannot be overridden or modified

Update the below code inside CourseFrameworks.java file

        final String pythonCode = "PY";
	final String javaCode = "JAVA";
	final String cSharpCode = "CS";
	final String angularCode = "NG";
	final String typeScriptCode = "TS";

Inside the MyFirstClass.java, add the below code

CourseFrameworks cf1= new CourseFrameworks("Angular", "Meterial Design");
		System.out.println(cf1.pythonCode);

If you try to initialize or update final attributes, it will throw error. For example if you try to update the value of cf1.pythonCode = "PYTHON"; , it says “The final field CourseFrameworks.pythonCode cannot be assigned”

For more information on Java OOP’s concepts you can refer Oracle official java documentation or tutorials.

Learn more about Java OOP’s concepts in our upcoming blog article.

Happy Learning!