As we know already JAVA is object oriented language. Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OO programming, computer programs are designed by making them out of objects that interact with one another.[1][2] There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type..An object-based Java application is a Java application whose design is based on declaring classes, creating objects from them, and designing interactions between those objects.
We already know five fundamentals of JAVA Abstraction, Encapsulation, Inheritance, Polymorphism, Class and Object.
A class is a template or blueprint for manufacturing objects. In technical word Class is a template which describe what are all state or behavior an instance of this class can have. Class is like you are creating a prototype of something and Object is actual implementation of that prototype. You declare a class by specifying the class keyword followed by a non-reserved identifier that names it. A pair of matching open and close brace characters ({ and }) follow and delimit the class's body. Let's take a look at the standard syntax for declaring a Java class:
By convention, the first letter of a class's name is uppercased and subsequent characters are lowercased (for example,Employee). If a name consists of multiple words, the first letter of each word is uppercased (such as SavingsAccount). This naming convention is called camelcasing.
In general, class declarations can include these components, in order:
- Modifiers such as public, private, and a number of others that you will encounter later.
- The class name, with the initial letter capitalized by convention.
- The class body, surrounded by braces, {}.
A class in java can contain:
- data member
- method
- constructor
- block
- class and interface
- What is Object
Let us now look deep into what are objects. If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior.
If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging, running
If you compare the software object with a real world object, they have very similar characteristics.
Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
- We always get confused with Class and Objects. And people always ask question what is difference between Class and Object. Class is like you are creating a prototype of something and Object is actual implementation of that prototype. In technical word Class is a template which describe what are all state or behavior an instance of this class can have. And Object is actual implementation of that class which have some state & behavior in form of variable and methods and it acquire some memory space in heap.
Example of Object and class that maintains the records of students
In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method on it. Here, we are displaying the state (data) of the objects by invoking the displayInformation method.
- classStudent2{
- int rollno;
- String name;
- void insertRecord(int r, String n){ //method
- rollno=r;
- name=n;
- }
- voiddisplayInformation(){System.out.println(rollno+" "+name);}//method
- publicstatic void main(String args[]){
- Student2 s1=newStudent2();
- Student2 s2=newStudent2();
- insertRecord(111,"Karan");
- insertRecord(222,"Aryan");
- displayInformation();
- displayInformation();
- }
- }
Public : It is an Access Modifier, which(AM) defines who can access this method(In Java World). Public means that this method will be accessible to any class(If other class can access this class.).
Static : Keyword which identifies the class related this. It means that this class is not instance related but class related. It can be accessed without creating the instance of Class.
Void : Return Type, It defined what this method can return. Which is void in this case it means that this method will not return any thing.
main: Name of the method. This method name is searched by JVM as starting point for an application.
String args[] : Parameter to main method.
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. Constructor is a block of code, which runs when you use new keyword in order to instantiate an object. It looks like a method, however it is not a method. Methods have return type but constructors don’t have any return type.
How to call a constructor? The constructor gets called when we create an object of a class (i.e. new keyword followed by class name). For e.g. Demo obj = new Demo(); (here Demo() is a default constructor of Demo class).
Default constructor: It is also known as no-arg constructor. Constructor with no arguments is known as default constructor.
class Demo
{
public Demo()
{
System.out.println("This is a default constructor");
}
}
Parameterized constructor: Constructor with argument list is known as parameterized constructor.
class Demo
{
public Demo(int num, String str)
{
System.out.println("This is a parameterized constructor");
}
}
The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.
Example of a constructor is given below:
class Example2{ private int var; public Example2() { //code for default one var = 10; } public Example2(int num) { //code for parameterized one var = num; } public int getValue() { return var; } public static void main(String args[]) { Example2 obj2 = new Example2(); System.out.println("var is: "+obj2.getValue()); }}
Output:
var is: 10Example2 obj2 = new Example2(77); System.out.println("var is: "+obj2.getValue());
Output:
var is: 77Example2 obj3 = new Example2(); Example2 obj2 = new Example2(77); System.out.println("var is: "+obj3.getValue());
Output:
var is: 10
Object Oriented Concepts
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. is the inclusion of behavior (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. Inheritance can also be defined as the process whereby one object acquires characteristics from one or more other objects the same way children acquire characteristics from their parents.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.Inheritance represents the IS-A relationship, also known as parent-child relationship.e.g Building ß- house house is a building Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses
extends key word. Composition
class Building{
.......
}
class House extends Building{
.........}
Why use inheritance in java
- For Method Overriding (so runtime polymorphism can be achieved).
Since in method overriding both the classes(base class and child class) have same method, compile doesn’t figure out which method to call at compile-time. In this case JVM(java virtual machine) decides which method to call at runtime that’s why it is known as runtime or dynamic polymorphism.
- For Code Reusability.
Syntax of Java Inheritance
- classSubclass-name extends Superclass-name
- {
- //methods and fields
- }
- Theextends keyword indicates that you are making a new class that derives from an existing class.
- In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
Example :
class A { |
2 | //Members and methods declarations. |
3 | } |
4 |
|
5 | class B extends A { |
6 | //Members and methods from A are inherited. |
7 | //Members and methods declarations of B. |
8 | }
Java supports only public inheritance and thus, all fields and methods of the superclass are inherited and can be used by the subclass. The only exception are the private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. In order to call the constructor of the superclass Java provides the keyword super, as shown below:
|
class A { |
02 | public A() { |
03 | System.out.println("New A"); |
04 | } |
05 | } |
06 |
|
07 | class B extends A { |
08 | public B() { |
09 | super(); |
10 | System.out.println("New B"); |
11 | } |
12 | }
|