Classes & Objects
In Java, a class is a blueprint or template to create objects. It defines the properties and behaviors that objects of that class will have.
Here the class student has two properties age and name.
In Java, an object is an instance of a class.
Here s1 is an object. "new" keyword is used to create new objects.
As we can see s1 has 2 properties age and name.
Methods
In Java, methods are blocks of code that perform specific tasks.
Polymorphism
Polymorphism is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass.
Constructor
In Java, a constructor is a special method used to create and initialize objects of a class. The constructor has the same name as the class and does not have a return type.
It is called when an object is created using the new keyword.
Static keyword
In Java, the static keyword is used to declare members that belong to the class itself, rather than to instances of the class.
this Keyword
In Java, this keyword is a reference to the current instance of a class.
Inheritance
Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit properties and behaviors from another class. Inheritance is achieved using the extends keyword.
We also use the super keyword in inheritance. This keyword calls the parent constructor.
Encapsulation
Packages
A package in Java is a way to organize related classes and interfaces into a single unit. Packages are declared at the beginning of a Java file using the package keyword.
Access Modifiers
Access modifiers in Java determine the visibility and accessibility of classes, methods, variables, and constructors. There are four access modifiers in Java:
The public access modifier allows unrestricted access from any other class or package.
The protected access modifier allows access within the same package and also from subclasses, even if they are in different packages.
The private access modifier restricts access within the same class only. It is used to encapsulate class internals and hide them from other classes.
When no access modifier is specified, it is considered the default access.
Now talking about encapsulation, it is used to hide the internal details of an object and provide a controlled and secure way to access and manipulate its data. Encapsulation is achieved by using access modifiers.
Here name gives an error as we used a private access modifier on the string name. So teacher class can't access string name and throws an error. We are restricting access to the name variable.
Abstraction
Abstraction is a fundamental principle in object-oriented programming that focuses on hiding complex implementation details and exposing only essential information to the outside world.
An abstract class in Java is a class that cannot be instantiated directly but can be subclassed. It can contain abstract methods as well as non-abstract methods. An abstract class is declared using the abstract keyword.
Interfaces
Interfaces in Java are a way to define a contract of methods that a class must implement. The interface is a completely abstract type that can be implemented by multiple classes.