coderz.py

Keep Coding Keep Cheering!

OOPs in Java

method overloading and method overriding
What is OOPs?

Object-Oriented Programming is a methodology or paradigm for designing a program using classes and objects. 

Many concepts, such as inheritance, data binding, polymorphism, and others, are available in Object-Oriented Programming. It takes a practical approach to solve a problem. As a result, object-oriented programming is a better and easier way to write programs than procedural programming languages like C, ALGOL, and PASCAL.

It simplifies software development and maintenance by providing some concepts:

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation
Classes in Java:

A class is a collection of objects. It is a logical entity.

A class can also be thought of as a blueprint for creating an individual object. Class does not take up any space.

A class instance is an object. It’s also known as a logical template class’s physical existence.

A class in Java can contain:

  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface
Syntax to declare a class:
class <class_name>{  
    field;  
    method;  
}  

Example:

public class Demo{ 
      String name;
      int roll_no;
      float percent;
      char grade;
}  

Instance variables are the fields declared within the class. When an object is created at runtime, it gets memory.

Methods in a class are similar to functions in that they perform operations and represent an object’s behavior.

Object in Java:

A chair, bike, marker, pen, table, car, or other entity with state and behaviour is known as an object. It could be either physical or logical (tangible and intangible). The banking system is an example of an intangible object.

There are three characteristics of an object:

  • The data (value) of an object is represented by its state.

  • The behaviour (functionality) of an object, such as deposit, withdrawal, and so on.
  • A unique ID is typically used to represent an object’s identity. The value of the ID is hidden from the outside user. However, the JVM uses it internally to uniquely identify each object.
Syntax to create an object:
className variable_name = new className();

We had already seen the various methods to create an object(Java object creation methods).

Object and Class Example: main within the class

We’ve created a Student class in this example, which has two data members: id and name. The object of the Student class is created using the new keyword, and the object’s value is printed.

We’re going to add a main() method to the class.

File: Student.java

class Student{  
 
 int id;//field or data member or instance variable  
 String name;  
 //creating main method inside the Student class  
 public static void main(String args[]){  
  //Creating an object or instance  
  Student s1=new Student();//creating an object of Student  
  //Printing values of the object  
  System.out.println(s1.id);//accessing member through reference variable  
  System.out.println(s1.name);  
 }  
}  

Output:

0 
null

Note: also read about the Command Line Argument in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement