What is Inheritance ?
Inheritance is a method in which one object acquires/inherits another object’s properties, and inheritance also supports hierarchical classification. The idea behind this is that we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. Inheritance represents the parent-child relationship.
Inheritance is a method in which one object acquires/inherits another object’s properties, and inheritance also supports hierarchical classification. The idea behind this is that we can create new classes built on existing classes, i.e., when you inherit from an existing class, we can reuse methods and fields of the parent class. Inheritance represents the parent-child relationship.
For example, a whale is a part of the classification of marine animals, which is part of class mammal, which is under that class of animal. We use hierarchical classification, i.e., top-down classification. If we want to describe a more specific class of animals such as mammals, they would have more specific attributes such as teeth; cold-blooded, warm-blooded, etc. This comes under the subclass of animals whereas animals come under the superclass. The subclass is a class which inherits properties of the superclass. This is also called a derived class. A superclass is a base class or parental class from which a subclass inherits properties.
We use inheritance mainly for method overriding and R:
To inherit a class, we use the extend keyword.
There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical.
We use inheritance mainly for method overriding and R:
To inherit a class, we use the extend keyword.
There are five types of inheritance single, multilevel, multiple, hybrid and hierarchical.
* Hybrid inheritance
This is the combination of multiple and multilevel inheritances and in java, multiple inheritances are not supported as it leads to ambiguity and this type of inheritance can only be achieved through interfaces.
Consider that class a is the parental or base class of class b and class c and in turn, class b and class c are parental or a base class of class d. Class b and class c are derived classes from class a and class d is derived class from class b and class c.
The following program creates a superclass called add and a subclass called sub, using extend keyword to create a subclass add.
This is the combination of multiple and multilevel inheritances and in java, multiple inheritances are not supported as it leads to ambiguity and this type of inheritance can only be achieved through interfaces.
Consider that class a is the parental or base class of class b and class c and in turn, class b and class c are parental or a base class of class d. Class b and class c are derived classes from class a and class d is derived class from class b and class c.
The following program creates a superclass called add and a subclass called sub, using extend keyword to create a subclass add.
// a simple example of inheritance
// create a superclass
class Add {
int my;
int by;
void setmyby (int xy, int hy) {
my=xy;
by=hy;
}
}
// create a sub class
class b extends add {
int total;
void sum() {
public Static void main(String args[]) {
b sub0b = new b();
sub0b.Setmyby(10,12);
sub0b.Sum();
System.out.println("total = " + sub0b.Total);
}
}
// create a superclass
class Add {
int my;
int by;
void setmyby (int xy, int hy) {
my=xy;
by=hy;
}
}
// create a sub class
class b extends add {
int total;
void sum() {
public Static void main(String args[]) {
b sub0b = new b();
sub0b.Setmyby(10,12);
sub0b.Sum();
System.out.println("total = " + sub0b.Total);
}
}
What is Polymorphism ?
Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. Polymorphism is of two different types, i.e., compile-time polymorphism and runtime polymorphism. One of the examples of Compile time polymorphism is that when we overload a static method in java. Run time polymorphism also called a dynamic method dispatch is a method in which a call to an overridden method is resolved at run time rather than compile time. In this method, the overridden method is always called through the reference variable. By using method overloading and method overriding, we can perform polymorphism. Generally, the concept of polymorphism is often expressed as one interface, and multiple methods. This reduces complexity by allowing the same interface to be used as a general class of action.
Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance. Polymorphism is of two different types, i.e., compile-time polymorphism and runtime polymorphism. One of the examples of Compile time polymorphism is that when we overload a static method in java. Run time polymorphism also called a dynamic method dispatch is a method in which a call to an overridden method is resolved at run time rather than compile time. In this method, the overridden method is always called through the reference variable. By using method overloading and method overriding, we can perform polymorphism. Generally, the concept of polymorphism is often expressed as one interface, and multiple methods. This reduces complexity by allowing the same interface to be used as a general class of action.
What is Encapsulation ?
Encapsulation is one of the concepts in OOPs concepts; it is the process that binds together the data and code into a single unit and keeps both from being safe from outside interference and misuse. In this process, the data is hidden from other classes and can be accessed only through the current class’s methods. Hence, it is also known as data hiding. Encapsulation acts as a protective wrapper that prevents the code and data from being accessed by outsiders. These are controlled through a well-defined interface.
Encapsulation is achieved by declaring the variables as private and providing public setter and getter methods to modify and view the variable values. In encapsulation, the fields of a class are made read-only or write-only. This method also improves reusability. Encapsulated code is also easy to test for unit testing.
Encapsulation is one of the concepts in OOPs concepts; it is the process that binds together the data and code into a single unit and keeps both from being safe from outside interference and misuse. In this process, the data is hidden from other classes and can be accessed only through the current class’s methods. Hence, it is also known as data hiding. Encapsulation acts as a protective wrapper that prevents the code and data from being accessed by outsiders. These are controlled through a well-defined interface.
Encapsulation is achieved by declaring the variables as private and providing public setter and getter methods to modify and view the variable values. In encapsulation, the fields of a class are made read-only or write-only. This method also improves reusability. Encapsulated code is also easy to test for unit testing.
class animal {
// private field
private int age'
// getter method
Public int gerage() {
return age;
}
// setter method
public void setAge (int age) {
this.Age = age;
}
}
class Main {
public static void main(String args[]);
// create an object of person
Animal a1= new Animal();
// change age using setter
A1.setAge(12);
//access age using getter
System.out.println("animal age is " + a1.getage());
}
}
// private field
private int age'
// getter method
Public int gerage() {
return age;
}
// setter method
public void setAge (int age) {
this.Age = age;
}
}
class Main {
public static void main(String args[]);
// create an object of person
Animal a1= new Animal();
// change age using setter
A1.setAge(12);
//access age using getter
System.out.println("animal age is " + a1.getage());
}
}
In this example, we declared a private field called age that cannot be accessed outside of the class.
To access age, we used public methods. These methods are called getter and setter methods. Making age private allows us to restrict unauthorized access from outside the class. Hence this is called data hiding.
To access age, we used public methods. These methods are called getter and setter methods. Making age private allows us to restrict unauthorized access from outside the class. Hence this is called data hiding.
Coupling in Java
Coupling refers to the relationship between two classes. It indicates the knowledge one object or class has of another. That means that if one class changes its properties or behaviour, it will affect the dependent changes in the other class. Therefore, these changes will depend upon the level of interdependence the two classes have between them. There are two types of coupling, namely tight coupling, and loose coupling.
Coupling refers to the relationship between two classes. It indicates the knowledge one object or class has of another. That means that if one class changes its properties or behaviour, it will affect the dependent changes in the other class. Therefore, these changes will depend upon the level of interdependence the two classes have between them. There are two types of coupling, namely tight coupling, and loose coupling.