The present paper studies a new class of problems of optimal control theory with linear second order self-adjoint Sturm-Liouville type differential operators and with functional and non-functional endpoint constraints. Sufficient conditions of optimality, containing both the second order Euler-Lagrange and Hamiltonian type inclusions are derived. The presence of functional constraints generates a special second order transversality inclusions and complementary slackness conditions peculiar to inequality constraints; this approach and results make a bridge between optimal control problem with Sturm-Liouville type differential differential inclusions and constrained mathematical programming problems in finite-dimensional spaces.The idea for obtaining optimality conditions is based on applying locally-adjoint mappings to Sturm-Liouville type set-valued mappings. The result generalizes to the problem with a second order non-self-adjoint differential operator. Furthermore, practical applications of these results are demonstrated by optimization of some semilinear optimal control problems for which the Pontryagin maximum condition is obtained. A numerical example is given to illustrate the feasibility and effectiveness of the theoretic results obtained.