In the interview, the usual question you face is regarding “Object-Oriented Programming” (OOP) which is associated with concepts like class, object, inheritance, encapsulation, abstraction, polymorphism, etc.
Java is one of the sought-after languages for beginners and professionals also. But the heck is for what classes, objects and methods are used? Why do we need inheritance and polymorphism? and more. At Java classes in Pune, an aspirant can learn everything about the OOPs, in detail.
-
Inheritance
In the Inheritance class properties of one class can be inherited by the other where reuse of code is possible to establish relationships between different classes. As we know in the biological system due to DNA child inherits some of the parent properties. Similarly, here “Child Class” (Subclass or derived class) inherits the properties from the “Parent Class” (Super or base class), inheritance is further classified into four types:
Let’s understand the concept with examples, where, Class A is your parent class and Class B is your child class
- Single Inheritance
Example
Class A {—}
Class B extends A {—}
- Multilevel Inheritance
Please find the example below, where, A is the parent class for B and class B is the parent class for C.
Example
Class A {—}
Class B extends A {—}
Class C extends B {—}
- Hierarchical Inheritance
Please find the example below, where, Class B and C are the child classes that are inheriting from the parent class i.e Class A.
Class A {—}
Class B extends A {—}
Class C extends A {—}
- Hybrid Inheritance
Class A and B extends class C → Hierarchical inheritance
Class D extends class A → Single inheritance
class C {
public void disp() {
System.out.println(“C”);
}}
class A extends C{
public void disp(){
System.out.println(“A”);
}}
class B extends C{
public void disp(){
System.out.println(“B”);
}}
class D extends A{
public void disp(){
System.out.println(“D”);
}
public static void main(String args[]){
D obj = new D();
obj.disp();
}}
-
Encapsulation
We can achieve encapsulation in Java by:
- Variable of the class are private.
- Public setter and getter methods are defined to modify and view the variable’s values.
Example
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
}}
-
Abstraction
At Java coaching classes in Pune, an aspirant will learn the abstraction concept of Java programming in two ways:
- Abstract Class
For the abstraction, you need to inherit the class from another class
The syntax of an abstract class:
Abstract class Mobile { // abstract class mobile
Abstract void run(); // abstract method
- Interface
In the abstraction class, an interface is a group of related methods with empty bodies.
Example
public interface ParentCar {
public void changeGear( int newValue);
public void speedUp(int increment);
public void applyBrakes(int decrement);
}
-
Polymorphism
Java training institutes in Pune is the best place to understand the keen concepts of Java programming with real-life examples. Polymorphism defines one interface or method with multiple implementations.
Polymorphism in Java is of two types:
- Runtime polymorphism
Example
public Class BowlerClass{
void bowlingMethod(){
System.out.println(” bowler “);
}
public Class FastPacer{
void bowlingMethod(){
System.out.println(” fast bowler “);
}
Public static void main(String[] args){
FastPacer obj= new FastPacer();
obj.bowlingMethod();
}}
- Compile time polymorphism
Example
class Adder {
Static int add(int a, int b){
return a+b;
}
static double add( double a, double b){
return a+b;
}
public static void main(String args[]){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
This will help the aspirant to get an overview of the Object-Oriented programming concept of Java. To learn in-detail Java programming with practical implementation, visit LotusITHub institute at any of our branches in Pune. You can meet us at Karve Nagar, Kothrud, Narhe, Warje, Dhayari in our office for a free demo session on any of software technology, aptitude, English speaking along with expert guidance. Got a question for us? Please visit our site for detail knowledge at www.lotusithub.com or visit us asap.