Class inheritance in JAVA (hierarchy classes)
Class inheritance is the process of building new classes on top of existing ones. A set of some object properties and behaviors that can be found in existing classes can be passed on to new classes through inheritance. The official Java language word "extends" is used for this.
The syntax is:
The syntax is:
public class Subclass_name extends Superclass_name
{
//Atributes
//Methods
}//Methods
The purpose of creating a subclass is to extend the existing state and behavior of the superclass. A "superclass" is also called a "parent" class, and a "subclass" is a "child" class.
Multiple classes can be in a hierarchy. If one class, e.g. Class_3, inherits another (Class_2), and this one inherits a previous one, e.g. class "Class_1", then class 3 inherits all previous properties and methods in the hierarchy.
If we were to create a hierarchy of classes representing cars it would look like this:
Multiple classes can be in a hierarchy. If one class, e.g. Class_3, inherits another (Class_2), and this one inherits a previous one, e.g. class "Class_1", then class 3 inherits all previous properties and methods in the hierarchy.
If we were to create a hierarchy of classes representing cars it would look like this:
VIDEO 1: Nasleđivanje klasa u JAVI
Let's observe, for example a class that describes a person. It should have the following attributes: first name, last name, gender, jmbg, as well as the toString method that returns all data as one String.
If we now want to create a class that describes a student (Student), we should notice that she will have all the attributes that are already listed in the Person class, because the student is actually a person and some other attributes in addition, class, success, etc.
Since Java supports the inheritance of classes, we can then make a Classroom Student by expanding the Class of Person, i.e. inherits its properties and methods. Instead, we could write the classroom Student from the beginning, and then rewrite the personality and methods from Person, but it's a worse way.
Here's what the classroom class that inherits the class will look like. Person:
Since Java supports the inheritance of classes, we can then make a Classroom Student by expanding the Class of Person, i.e. inherits its properties and methods. Instead, we could write the classroom Student from the beginning, and then rewrite the personality and methods from Person, but it's a worse way.
Here's what the classroom class that inherits the class will look like. Person:
Note that no attributes are found in the class Person: name, surname, ... However, when a class object is created, the student will have these attributes as well, since they are inherited from the Person class. We can check this by creating a student object and when we put an operator "after" the object, we will see all the attributes of the student object:
Class, school, and success are prominent because they are specific to the class Student. The name, surname, jmbg, sex, and toString () are inherited from the People class.
Each class, whatever it explicitly does not write, inherits the Object class, which has methods in it: equals (), hashCode (), notify () etc., ie. all the other methods shown in the picture above.
If we invoke the toString () method for a student object, the toString () method will be called in the Parent class, class Person, which is logical, because it is inherited. It will return the person's data: name, surname, etc. but not class, success. In class inheritance, there is a possibility that this same method is rewritten (override) in the Classroom class by returning data on the class, success. Then, the student.toString () method call is a recalled method instead of the inherited toString () method from the Person class. Here's an example of using this method:
Defining of:
Each class, whatever it explicitly does not write, inherits the Object class, which has methods in it: equals (), hashCode (), notify () etc., ie. all the other methods shown in the picture above.
If we invoke the toString () method for a student object, the toString () method will be called in the Parent class, class Person, which is logical, because it is inherited. It will return the person's data: name, surname, etc. but not class, success. In class inheritance, there is a possibility that this same method is rewritten (override) in the Classroom class by returning data on the class, success. Then, the student.toString () method call is a recalled method instead of the inherited toString () method from the Person class. Here's an example of using this method:
Defining of:
In the test class "School" we create some objects:
After running:
Access to the attributes and methods of the parent class
If from a class in the example, for example, Classroom Student We want to access the attribute or the method that is in the parent class can be done using the official word super.
The use of this word can be seen with the constructor with parameters of class Student
The use of this word can be seen with the constructor with parameters of class Student
Inside the constructor, the Constructor invokes the constructor of the Person using the word super, and the parameters for the Person class are passed to him, and that is the name, surname ...
Another example is the toString () method:
Another example is the toString () method:
It should be noted that the person's data will be obtained by invoking the method of the toString () class of persons. The object that represents the person here is actually the official word "super".
Famous class hierarchies
An example of a class hierarchy from the APIs is in the SWING package, which are JButton, JLabel, JTextField .. classes. those classes that represent components in the form. Here is an example of such a hierarchy:
Swing components inherit the JComponent class, and one such example can be seen in the following figure:
Java and Physics
Consider the following problem:
We want to simulate the change of position x in the action of force on a body standing on a horizontal support. We observe position changes for every small increase in time from dt [s]. Suppose this change is for dt = 0.05s. A body of mass m will receive an acceleration in the x direction ax from the rest state if F> Ftr. The body of mass m will not move in the y direction (ay = 0) because N = Fg wherein: Fg - Body weight N-reaction of the substrate To simulate motion, we need forces acting on the body. We will create one object for each force acting on the body: F, Ftr, N and Fg |
We will create one object for each force acting on the body:
F, Ftr, N and Fg
To create objects, we need classes to describe them:
Let us first make classes for the Body weight G and the friction force Ftr.
F, Ftr, N and Fg
To create objects, we need classes to describe them:
Let us first make classes for the Body weight G and the friction force Ftr.
public class WeightOfBody
{
double intensity, direction, m;
String name;
final double G=9.81;
public WeightOfBody(double direction, double m)
{
this.direction= direction;
this.m = m;
}
public void intensity()
{
intensity=m*G;
}
public void setName(String name)
{
this.name=name;
}
}
{
double intensity, direction, m;
String name;
final double G=9.81;
public WeightOfBody(double direction, double m)
{
this.direction= direction;
this.m = m;
}
public void intensity()
{
intensity=m*G;
}
public void setName(String name)
{
this.name=name;
}
}
public class ForceFriction
{
double intensity, coefOfFriction, N;
int direction;
String naziv;
public ForceFriction(double coefOfFriction, double N)
{
this.coefOfFriction= coefOfFriction;
N = n;
}
public void intensity()
{
intensity=coefOfFriction*N;
}
public void setName(String name)
{
this.name=name;
}
}
{
double intensity, coefOfFriction, N;
int direction;
String naziv;
public ForceFriction(double coefOfFriction, double N)
{
this.coefOfFriction= coefOfFriction;
N = n;
}
public void intensity()
{
intensity=coefOfFriction*N;
}
public void setName(String name)
{
this.name=name;
}
}
Let's note the following:
- Both classes have the same properties as name, intensity and coefficient, as well as the setName () method;
- For both classes, there is an intensity () method, but the commands inside are different.
- Other features and methods are specific to the force of gravity only and some to the force of friction only.
Should these commonalities be separated into a separate class.
public class Force
{
double intensity;
int direction;
String name;
public Force(int direction)
{
this.direction= direction;
}
public void setName(String name)
{
this.name=name;
}
}
{
double intensity;
int direction;
String name;
public Force(int direction)
{
this.direction= direction;
}
public void setName(String name)
{
this.name=name;
}
}
Then, the WeightOfBody class, as well as the FrictionForce, inherit the Force class. After the class name should be added
extends Force
extends Force
public class WeightOfBody extends Force
{
double m;
final doubleG=9.81;
public WeightOfBody(int direction, double m)
{
super(smer);
this.direction= direction;
}
public void intensity()
{
intensity=m*G;
}
}
{
double m;
final doubleG=9.81;
public WeightOfBody(int direction, double m)
{
super(smer);
this.direction= direction;
}
public void intensity()
{
intensity=m*G;
}
}
Data name and intensity
as well as the setName () method
who were transferred to the Force class are now inherited and will be assigned to WeightOfBody class objects
as well as the setName () method
who were transferred to the Force class are now inherited and will be assigned to WeightOfBody class objects
In order to test these classes now, we must create an object in the test class that represents the force of gravity. The object will have all the attributes and methods described in the class to which the object belongs. To select one of the properties or methods of the created object, we use the operator "." as pictured below.
In the window that opened, when we put a point after the object reference, we see the properties and methods, both those directly written in the WeightOfBody class and all those inherited from the previous classes in the hierarchy. So e.g. intensity is an inherited property from the Force class, while toString () is an inherited method from the Object class.
Example: For a body mass of m = 3kg, create an object of gravity, give it a name, calculate the intensity of the force and display it on the screen.
public class ForceAnalysisTest
{
public static void main(String [](args )
{
WeightOfBodyweight = new WeightOfBody(1,3);
weight.setName("The weight of body");
weight.intensity();
System.out.println( weight.name+" is "+weight.intensity()+" N");
}
}
{
public static void main(String [](args )
{
WeightOfBodyweight = new WeightOfBody(1,3);
weight.setName("The weight of body");
weight.intensity();
System.out.println( weight.name+" is "+weight.intensity()+" N");
}
}
First, a thesis object belonging to the WeightOfBody class was created, where a constructor with parameters was used, where vector direction and body mass were sent as parameters. The setName () and Intensity () methods are then called via the object reference. Intensity method is a method that calculates intensity but has no returning data(type is void). The last step shows the calculated values on standard output.
Method Overriding
All classes inherit the Object class, so all of its methods are inherited.
The toString () method is one of the inherited methods in the Object class, and is intended to form text to display data from the object to which it belongs.
However, there is nothing in the body of this method that is written in the Object class because there is no data in that class, so the toString () method is rewritten in the class that inherits it, in our case it is Force and WeightOfBody.
The toString () method is one of the inherited methods in the Object class, and is intended to form text to display data from the object to which it belongs.
However, there is nothing in the body of this method that is written in the Object class because there is no data in that class, so the toString () method is rewritten in the class that inherits it, in our case it is Force and WeightOfBody.
We were offered the Object class here because we inherited it. We click toString () from the offered methods and click the OK button.
After closing the dialog, the toString method was generated and now, in the body of the method, we need to adjust it to show force data.
We replace the selected super.toString () with name + "equals" + intensity + "[N]" |
Example 2: The body of mass m is stationary on a horizontal surface and at a moment a constant force F in the x-axis begins to act on it; Determine the position of the center of mass after the time t in relation to the initial position. The coefficient of friction is 0.05.
Create objects that represent all the forces on the body in succession using class inheritance. Print out the intensities of all forces. Taken as positive intensities if acting in the direction of the co-axes.
Previous
|< Classes and Objects |
Next
Abstract classes and interfaces >| |